1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Holt Integrated Circuits HI-8435 threshold detector driver
5 * Copyright (C) 2015 Zodiac Inflight Innovations
6 * Copyright (C) 2015 Cogent Embedded, Inc.
9 #include <linux/delay.h>
10 #include <linux/iio/events.h>
11 #include <linux/iio/iio.h>
12 #include <linux/iio/sysfs.h>
13 #include <linux/iio/trigger.h>
14 #include <linux/iio/trigger_consumer.h>
15 #include <linux/iio/triggered_event.h>
16 #include <linux/interrupt.h>
17 #include <linux/module.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/spi/spi.h>
20 #include <linux/gpio/consumer.h>
22 #define DRV_NAME "hi8435"
24 /* Register offsets for HI-8435 */
25 #define HI8435_CTRL_REG 0x02
26 #define HI8435_PSEN_REG 0x04
27 #define HI8435_TMDATA_REG 0x1E
28 #define HI8435_GOCENHYS_REG 0x3A
29 #define HI8435_SOCENHYS_REG 0x3C
30 #define HI8435_SO7_0_REG 0x10
31 #define HI8435_SO15_8_REG 0x12
32 #define HI8435_SO23_16_REG 0x14
33 #define HI8435_SO31_24_REG 0x16
34 #define HI8435_SO31_0_REG 0x78
36 #define HI8435_WRITE_OPCODE 0x00
37 #define HI8435_READ_OPCODE 0x80
39 /* CTRL register bits */
40 #define HI8435_CTRL_TEST 0x01
41 #define HI8435_CTRL_SRST 0x02
44 struct spi_device *spi;
47 unsigned long event_scan_mask; /* soft mask/unmask channels events */
48 unsigned int event_prev_val;
50 unsigned threshold_lo[2]; /* GND-Open and Supply-Open thresholds */
51 unsigned threshold_hi[2]; /* GND-Open and Supply-Open thresholds */
52 u8 reg_buffer[3] __aligned(IIO_DMA_MINALIGN);
55 static int hi8435_readb(struct hi8435_priv *priv, u8 reg, u8 *val)
57 reg |= HI8435_READ_OPCODE;
58 return spi_write_then_read(priv->spi, ®, 1, val, 1);
61 static int hi8435_readw(struct hi8435_priv *priv, u8 reg, u16 *val)
66 reg |= HI8435_READ_OPCODE;
67 ret = spi_write_then_read(priv->spi, ®, 1, &be_val, 2);
68 *val = be16_to_cpu(be_val);
73 static int hi8435_readl(struct hi8435_priv *priv, u8 reg, u32 *val)
78 reg |= HI8435_READ_OPCODE;
79 ret = spi_write_then_read(priv->spi, ®, 1, &be_val, 4);
80 *val = be32_to_cpu(be_val);
85 static int hi8435_writeb(struct hi8435_priv *priv, u8 reg, u8 val)
87 priv->reg_buffer[0] = reg | HI8435_WRITE_OPCODE;
88 priv->reg_buffer[1] = val;
90 return spi_write(priv->spi, priv->reg_buffer, 2);
93 static int hi8435_writew(struct hi8435_priv *priv, u8 reg, u16 val)
95 priv->reg_buffer[0] = reg | HI8435_WRITE_OPCODE;
96 priv->reg_buffer[1] = (val >> 8) & 0xff;
97 priv->reg_buffer[2] = val & 0xff;
99 return spi_write(priv->spi, priv->reg_buffer, 3);
102 static int hi8435_read_raw(struct iio_dev *idev,
103 const struct iio_chan_spec *chan,
104 int *val, int *val2, long mask)
106 struct hi8435_priv *priv = iio_priv(idev);
111 case IIO_CHAN_INFO_RAW:
112 ret = hi8435_readl(priv, HI8435_SO31_0_REG, &tmp);
115 *val = !!(tmp & BIT(chan->channel));
122 static int hi8435_read_event_config(struct iio_dev *idev,
123 const struct iio_chan_spec *chan,
124 enum iio_event_type type,
125 enum iio_event_direction dir)
127 struct hi8435_priv *priv = iio_priv(idev);
129 return !!(priv->event_scan_mask & BIT(chan->channel));
132 static int hi8435_write_event_config(struct iio_dev *idev,
133 const struct iio_chan_spec *chan,
134 enum iio_event_type type,
135 enum iio_event_direction dir, int state)
137 struct hi8435_priv *priv = iio_priv(idev);
142 ret = hi8435_readl(priv, HI8435_SO31_0_REG, &tmp);
145 if (tmp & BIT(chan->channel))
146 priv->event_prev_val |= BIT(chan->channel);
148 priv->event_prev_val &= ~BIT(chan->channel);
150 priv->event_scan_mask |= BIT(chan->channel);
152 priv->event_scan_mask &= ~BIT(chan->channel);
157 static int hi8435_read_event_value(struct iio_dev *idev,
158 const struct iio_chan_spec *chan,
159 enum iio_event_type type,
160 enum iio_event_direction dir,
161 enum iio_event_info info,
164 struct hi8435_priv *priv = iio_priv(idev);
169 ret = hi8435_readb(priv, HI8435_PSEN_REG, &psen);
173 /* Supply-Open or GND-Open sensing mode */
174 mode = !!(psen & BIT(chan->channel / 8));
176 ret = hi8435_readw(priv, mode ? HI8435_SOCENHYS_REG :
177 HI8435_GOCENHYS_REG, ®);
181 if (dir == IIO_EV_DIR_FALLING)
182 *val = ((reg & 0xff) - (reg >> 8)) / 2;
183 else if (dir == IIO_EV_DIR_RISING)
184 *val = ((reg & 0xff) + (reg >> 8)) / 2;
189 static int hi8435_write_event_value(struct iio_dev *idev,
190 const struct iio_chan_spec *chan,
191 enum iio_event_type type,
192 enum iio_event_direction dir,
193 enum iio_event_info info,
196 struct hi8435_priv *priv = iio_priv(idev);
201 ret = hi8435_readb(priv, HI8435_PSEN_REG, &psen);
205 /* Supply-Open or GND-Open sensing mode */
206 mode = !!(psen & BIT(chan->channel / 8));
208 ret = hi8435_readw(priv, mode ? HI8435_SOCENHYS_REG :
209 HI8435_GOCENHYS_REG, ®);
213 if (dir == IIO_EV_DIR_FALLING) {
214 /* falling threshold range 2..21V, hysteresis minimum 2V */
215 if (val < 2 || val > 21 || (val + 2) > priv->threshold_hi[mode])
218 if (val == priv->threshold_lo[mode])
221 priv->threshold_lo[mode] = val;
223 /* hysteresis must not be odd */
224 if ((priv->threshold_hi[mode] - priv->threshold_lo[mode]) % 2)
225 priv->threshold_hi[mode]--;
226 } else if (dir == IIO_EV_DIR_RISING) {
227 /* rising threshold range 3..22V, hysteresis minimum 2V */
228 if (val < 3 || val > 22 || val < (priv->threshold_lo[mode] + 2))
231 if (val == priv->threshold_hi[mode])
234 priv->threshold_hi[mode] = val;
236 /* hysteresis must not be odd */
237 if ((priv->threshold_hi[mode] - priv->threshold_lo[mode]) % 2)
238 priv->threshold_lo[mode]++;
241 /* program thresholds */
242 mutex_lock(&priv->lock);
244 ret = hi8435_readw(priv, mode ? HI8435_SOCENHYS_REG :
245 HI8435_GOCENHYS_REG, ®);
247 mutex_unlock(&priv->lock);
252 reg = priv->threshold_hi[mode] - priv->threshold_lo[mode];
254 /* threshold center */
255 reg |= (priv->threshold_hi[mode] + priv->threshold_lo[mode]);
257 ret = hi8435_writew(priv, mode ? HI8435_SOCENHYS_REG :
258 HI8435_GOCENHYS_REG, reg);
260 mutex_unlock(&priv->lock);
265 static int hi8435_debugfs_reg_access(struct iio_dev *idev,
266 unsigned reg, unsigned writeval,
269 struct hi8435_priv *priv = iio_priv(idev);
273 if (readval != NULL) {
274 ret = hi8435_readb(priv, reg, &val);
278 ret = hi8435_writeb(priv, reg, val);
284 static const struct iio_event_spec hi8435_events[] = {
286 .type = IIO_EV_TYPE_THRESH,
287 .dir = IIO_EV_DIR_RISING,
288 .mask_separate = BIT(IIO_EV_INFO_VALUE),
290 .type = IIO_EV_TYPE_THRESH,
291 .dir = IIO_EV_DIR_FALLING,
292 .mask_separate = BIT(IIO_EV_INFO_VALUE),
294 .type = IIO_EV_TYPE_THRESH,
295 .dir = IIO_EV_DIR_EITHER,
296 .mask_separate = BIT(IIO_EV_INFO_ENABLE),
300 static int hi8435_get_sensing_mode(struct iio_dev *idev,
301 const struct iio_chan_spec *chan)
303 struct hi8435_priv *priv = iio_priv(idev);
307 ret = hi8435_readb(priv, HI8435_PSEN_REG, ®);
311 return !!(reg & BIT(chan->channel / 8));
314 static int hi8435_set_sensing_mode(struct iio_dev *idev,
315 const struct iio_chan_spec *chan,
318 struct hi8435_priv *priv = iio_priv(idev);
322 mutex_lock(&priv->lock);
324 ret = hi8435_readb(priv, HI8435_PSEN_REG, ®);
326 mutex_unlock(&priv->lock);
330 reg &= ~BIT(chan->channel / 8);
332 reg |= BIT(chan->channel / 8);
334 ret = hi8435_writeb(priv, HI8435_PSEN_REG, reg);
336 mutex_unlock(&priv->lock);
341 static const char * const hi8435_sensing_modes[] = { "GND-Open",
344 static const struct iio_enum hi8435_sensing_mode = {
345 .items = hi8435_sensing_modes,
346 .num_items = ARRAY_SIZE(hi8435_sensing_modes),
347 .get = hi8435_get_sensing_mode,
348 .set = hi8435_set_sensing_mode,
351 static const struct iio_chan_spec_ext_info hi8435_ext_info[] = {
352 IIO_ENUM("sensing_mode", IIO_SEPARATE, &hi8435_sensing_mode),
353 IIO_ENUM_AVAILABLE("sensing_mode", IIO_SHARED_BY_TYPE, &hi8435_sensing_mode),
357 #define HI8435_VOLTAGE_CHANNEL(num) \
359 .type = IIO_VOLTAGE, \
362 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
363 .event_spec = hi8435_events, \
364 .num_event_specs = ARRAY_SIZE(hi8435_events), \
365 .ext_info = hi8435_ext_info, \
368 static const struct iio_chan_spec hi8435_channels[] = {
369 HI8435_VOLTAGE_CHANNEL(0),
370 HI8435_VOLTAGE_CHANNEL(1),
371 HI8435_VOLTAGE_CHANNEL(2),
372 HI8435_VOLTAGE_CHANNEL(3),
373 HI8435_VOLTAGE_CHANNEL(4),
374 HI8435_VOLTAGE_CHANNEL(5),
375 HI8435_VOLTAGE_CHANNEL(6),
376 HI8435_VOLTAGE_CHANNEL(7),
377 HI8435_VOLTAGE_CHANNEL(8),
378 HI8435_VOLTAGE_CHANNEL(9),
379 HI8435_VOLTAGE_CHANNEL(10),
380 HI8435_VOLTAGE_CHANNEL(11),
381 HI8435_VOLTAGE_CHANNEL(12),
382 HI8435_VOLTAGE_CHANNEL(13),
383 HI8435_VOLTAGE_CHANNEL(14),
384 HI8435_VOLTAGE_CHANNEL(15),
385 HI8435_VOLTAGE_CHANNEL(16),
386 HI8435_VOLTAGE_CHANNEL(17),
387 HI8435_VOLTAGE_CHANNEL(18),
388 HI8435_VOLTAGE_CHANNEL(19),
389 HI8435_VOLTAGE_CHANNEL(20),
390 HI8435_VOLTAGE_CHANNEL(21),
391 HI8435_VOLTAGE_CHANNEL(22),
392 HI8435_VOLTAGE_CHANNEL(23),
393 HI8435_VOLTAGE_CHANNEL(24),
394 HI8435_VOLTAGE_CHANNEL(25),
395 HI8435_VOLTAGE_CHANNEL(26),
396 HI8435_VOLTAGE_CHANNEL(27),
397 HI8435_VOLTAGE_CHANNEL(28),
398 HI8435_VOLTAGE_CHANNEL(29),
399 HI8435_VOLTAGE_CHANNEL(30),
400 HI8435_VOLTAGE_CHANNEL(31),
401 IIO_CHAN_SOFT_TIMESTAMP(32),
404 static const struct iio_info hi8435_info = {
405 .read_raw = hi8435_read_raw,
406 .read_event_config = hi8435_read_event_config,
407 .write_event_config = hi8435_write_event_config,
408 .read_event_value = hi8435_read_event_value,
409 .write_event_value = hi8435_write_event_value,
410 .debugfs_reg_access = hi8435_debugfs_reg_access,
413 static void hi8435_iio_push_event(struct iio_dev *idev, unsigned int val)
415 struct hi8435_priv *priv = iio_priv(idev);
416 enum iio_event_direction dir;
418 unsigned int status = priv->event_prev_val ^ val;
423 for_each_set_bit(i, &priv->event_scan_mask, 32) {
424 if (status & BIT(i)) {
425 dir = val & BIT(i) ? IIO_EV_DIR_RISING :
428 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, i,
429 IIO_EV_TYPE_THRESH, dir),
430 iio_get_time_ns(idev));
434 priv->event_prev_val = val;
437 static irqreturn_t hi8435_trigger_handler(int irq, void *private)
439 struct iio_poll_func *pf = private;
440 struct iio_dev *idev = pf->indio_dev;
441 struct hi8435_priv *priv = iio_priv(idev);
445 ret = hi8435_readl(priv, HI8435_SO31_0_REG, &val);
449 hi8435_iio_push_event(idev, val);
452 iio_trigger_notify_done(idev->trig);
457 static void hi8435_triggered_event_cleanup(void *data)
459 iio_triggered_event_cleanup(data);
462 static int hi8435_probe(struct spi_device *spi)
464 struct iio_dev *idev;
465 struct hi8435_priv *priv;
466 struct gpio_desc *reset_gpio;
469 idev = devm_iio_device_alloc(&spi->dev, sizeof(*priv));
473 priv = iio_priv(idev);
476 reset_gpio = devm_gpiod_get(&spi->dev, NULL, GPIOD_OUT_LOW);
477 if (IS_ERR(reset_gpio)) {
478 /* chip s/w reset if h/w reset failed */
479 hi8435_writeb(priv, HI8435_CTRL_REG, HI8435_CTRL_SRST);
480 hi8435_writeb(priv, HI8435_CTRL_REG, 0);
483 gpiod_set_value_cansleep(reset_gpio, 1);
486 mutex_init(&priv->lock);
488 idev->name = spi_get_device_id(spi)->name;
489 idev->modes = INDIO_DIRECT_MODE;
490 idev->info = &hi8435_info;
491 idev->channels = hi8435_channels;
492 idev->num_channels = ARRAY_SIZE(hi8435_channels);
494 /* unmask all events */
495 priv->event_scan_mask = ~(0);
497 * There is a restriction in the chip - the hysteresis can not be odd.
498 * If the hysteresis is set to odd value then chip gets into lock state
499 * and not functional anymore.
500 * After chip reset the thresholds are in undefined state, so we need to
501 * initialize thresholds to some initial values and then prevent
502 * userspace setting odd hysteresis.
504 * Set threshold low voltage to 2V, threshold high voltage to 4V
505 * for both GND-Open and Supply-Open sensing modes.
507 priv->threshold_lo[0] = priv->threshold_lo[1] = 2;
508 priv->threshold_hi[0] = priv->threshold_hi[1] = 4;
509 hi8435_writew(priv, HI8435_GOCENHYS_REG, 0x206);
510 hi8435_writew(priv, HI8435_SOCENHYS_REG, 0x206);
512 ret = iio_triggered_event_setup(idev, NULL, hi8435_trigger_handler);
516 ret = devm_add_action_or_reset(&spi->dev,
517 hi8435_triggered_event_cleanup,
522 return devm_iio_device_register(&spi->dev, idev);
525 static const struct of_device_id hi8435_dt_ids[] = {
526 { .compatible = "holt,hi8435" },
529 MODULE_DEVICE_TABLE(of, hi8435_dt_ids);
531 static const struct spi_device_id hi8435_id[] = {
535 MODULE_DEVICE_TABLE(spi, hi8435_id);
537 static struct spi_driver hi8435_driver = {
540 .of_match_table = hi8435_dt_ids,
542 .probe = hi8435_probe,
543 .id_table = hi8435_id,
545 module_spi_driver(hi8435_driver);
547 MODULE_LICENSE("GPL");
548 MODULE_AUTHOR("Vladimir Barinov");
549 MODULE_DESCRIPTION("HI-8435 threshold detector");