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>
19 #include <linux/of_device.h>
20 #include <linux/of_gpio.h>
21 #include <linux/spi/spi.h>
22 #include <linux/gpio/consumer.h>
24 #define DRV_NAME "hi8435"
26 /* Register offsets for HI-8435 */
27 #define HI8435_CTRL_REG 0x02
28 #define HI8435_PSEN_REG 0x04
29 #define HI8435_TMDATA_REG 0x1E
30 #define HI8435_GOCENHYS_REG 0x3A
31 #define HI8435_SOCENHYS_REG 0x3C
32 #define HI8435_SO7_0_REG 0x10
33 #define HI8435_SO15_8_REG 0x12
34 #define HI8435_SO23_16_REG 0x14
35 #define HI8435_SO31_24_REG 0x16
36 #define HI8435_SO31_0_REG 0x78
38 #define HI8435_WRITE_OPCODE 0x00
39 #define HI8435_READ_OPCODE 0x80
41 /* CTRL register bits */
42 #define HI8435_CTRL_TEST 0x01
43 #define HI8435_CTRL_SRST 0x02
46 struct spi_device
*spi
;
49 unsigned long event_scan_mask
; /* soft mask/unmask channels events */
50 unsigned int event_prev_val
;
52 unsigned threshold_lo
[2]; /* GND-Open and Supply-Open thresholds */
53 unsigned threshold_hi
[2]; /* GND-Open and Supply-Open thresholds */
54 u8 reg_buffer
[3] ____cacheline_aligned
;
57 static int hi8435_readb(struct hi8435_priv
*priv
, u8 reg
, u8
*val
)
59 reg
|= HI8435_READ_OPCODE
;
60 return spi_write_then_read(priv
->spi
, ®
, 1, val
, 1);
63 static int hi8435_readw(struct hi8435_priv
*priv
, u8 reg
, u16
*val
)
68 reg
|= HI8435_READ_OPCODE
;
69 ret
= spi_write_then_read(priv
->spi
, ®
, 1, &be_val
, 2);
70 *val
= be16_to_cpu(be_val
);
75 static int hi8435_readl(struct hi8435_priv
*priv
, u8 reg
, u32
*val
)
80 reg
|= HI8435_READ_OPCODE
;
81 ret
= spi_write_then_read(priv
->spi
, ®
, 1, &be_val
, 4);
82 *val
= be32_to_cpu(be_val
);
87 static int hi8435_writeb(struct hi8435_priv
*priv
, u8 reg
, u8 val
)
89 priv
->reg_buffer
[0] = reg
| HI8435_WRITE_OPCODE
;
90 priv
->reg_buffer
[1] = val
;
92 return spi_write(priv
->spi
, priv
->reg_buffer
, 2);
95 static int hi8435_writew(struct hi8435_priv
*priv
, u8 reg
, u16 val
)
97 priv
->reg_buffer
[0] = reg
| HI8435_WRITE_OPCODE
;
98 priv
->reg_buffer
[1] = (val
>> 8) & 0xff;
99 priv
->reg_buffer
[2] = val
& 0xff;
101 return spi_write(priv
->spi
, priv
->reg_buffer
, 3);
104 static int hi8435_read_raw(struct iio_dev
*idev
,
105 const struct iio_chan_spec
*chan
,
106 int *val
, int *val2
, long mask
)
108 struct hi8435_priv
*priv
= iio_priv(idev
);
113 case IIO_CHAN_INFO_RAW
:
114 ret
= hi8435_readl(priv
, HI8435_SO31_0_REG
, &tmp
);
117 *val
= !!(tmp
& BIT(chan
->channel
));
124 static int hi8435_read_event_config(struct iio_dev
*idev
,
125 const struct iio_chan_spec
*chan
,
126 enum iio_event_type type
,
127 enum iio_event_direction dir
)
129 struct hi8435_priv
*priv
= iio_priv(idev
);
131 return !!(priv
->event_scan_mask
& BIT(chan
->channel
));
134 static int hi8435_write_event_config(struct iio_dev
*idev
,
135 const struct iio_chan_spec
*chan
,
136 enum iio_event_type type
,
137 enum iio_event_direction dir
, int state
)
139 struct hi8435_priv
*priv
= iio_priv(idev
);
144 ret
= hi8435_readl(priv
, HI8435_SO31_0_REG
, &tmp
);
147 if (tmp
& BIT(chan
->channel
))
148 priv
->event_prev_val
|= BIT(chan
->channel
);
150 priv
->event_prev_val
&= ~BIT(chan
->channel
);
152 priv
->event_scan_mask
|= BIT(chan
->channel
);
154 priv
->event_scan_mask
&= ~BIT(chan
->channel
);
159 static int hi8435_read_event_value(struct iio_dev
*idev
,
160 const struct iio_chan_spec
*chan
,
161 enum iio_event_type type
,
162 enum iio_event_direction dir
,
163 enum iio_event_info info
,
166 struct hi8435_priv
*priv
= iio_priv(idev
);
171 ret
= hi8435_readb(priv
, HI8435_PSEN_REG
, &psen
);
175 /* Supply-Open or GND-Open sensing mode */
176 mode
= !!(psen
& BIT(chan
->channel
/ 8));
178 ret
= hi8435_readw(priv
, mode
? HI8435_SOCENHYS_REG
:
179 HI8435_GOCENHYS_REG
, ®
);
183 if (dir
== IIO_EV_DIR_FALLING
)
184 *val
= ((reg
& 0xff) - (reg
>> 8)) / 2;
185 else if (dir
== IIO_EV_DIR_RISING
)
186 *val
= ((reg
& 0xff) + (reg
>> 8)) / 2;
191 static int hi8435_write_event_value(struct iio_dev
*idev
,
192 const struct iio_chan_spec
*chan
,
193 enum iio_event_type type
,
194 enum iio_event_direction dir
,
195 enum iio_event_info info
,
198 struct hi8435_priv
*priv
= iio_priv(idev
);
203 ret
= hi8435_readb(priv
, HI8435_PSEN_REG
, &psen
);
207 /* Supply-Open or GND-Open sensing mode */
208 mode
= !!(psen
& BIT(chan
->channel
/ 8));
210 ret
= hi8435_readw(priv
, mode
? HI8435_SOCENHYS_REG
:
211 HI8435_GOCENHYS_REG
, ®
);
215 if (dir
== IIO_EV_DIR_FALLING
) {
216 /* falling threshold range 2..21V, hysteresis minimum 2V */
217 if (val
< 2 || val
> 21 || (val
+ 2) > priv
->threshold_hi
[mode
])
220 if (val
== priv
->threshold_lo
[mode
])
223 priv
->threshold_lo
[mode
] = val
;
225 /* hysteresis must not be odd */
226 if ((priv
->threshold_hi
[mode
] - priv
->threshold_lo
[mode
]) % 2)
227 priv
->threshold_hi
[mode
]--;
228 } else if (dir
== IIO_EV_DIR_RISING
) {
229 /* rising threshold range 3..22V, hysteresis minimum 2V */
230 if (val
< 3 || val
> 22 || val
< (priv
->threshold_lo
[mode
] + 2))
233 if (val
== priv
->threshold_hi
[mode
])
236 priv
->threshold_hi
[mode
] = val
;
238 /* hysteresis must not be odd */
239 if ((priv
->threshold_hi
[mode
] - priv
->threshold_lo
[mode
]) % 2)
240 priv
->threshold_lo
[mode
]++;
243 /* program thresholds */
244 mutex_lock(&priv
->lock
);
246 ret
= hi8435_readw(priv
, mode
? HI8435_SOCENHYS_REG
:
247 HI8435_GOCENHYS_REG
, ®
);
249 mutex_unlock(&priv
->lock
);
254 reg
= priv
->threshold_hi
[mode
] - priv
->threshold_lo
[mode
];
256 /* threshold center */
257 reg
|= (priv
->threshold_hi
[mode
] + priv
->threshold_lo
[mode
]);
259 ret
= hi8435_writew(priv
, mode
? HI8435_SOCENHYS_REG
:
260 HI8435_GOCENHYS_REG
, reg
);
262 mutex_unlock(&priv
->lock
);
267 static int hi8435_debugfs_reg_access(struct iio_dev
*idev
,
268 unsigned reg
, unsigned writeval
,
271 struct hi8435_priv
*priv
= iio_priv(idev
);
275 if (readval
!= NULL
) {
276 ret
= hi8435_readb(priv
, reg
, &val
);
280 ret
= hi8435_writeb(priv
, reg
, val
);
286 static const struct iio_event_spec hi8435_events
[] = {
288 .type
= IIO_EV_TYPE_THRESH
,
289 .dir
= IIO_EV_DIR_RISING
,
290 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
292 .type
= IIO_EV_TYPE_THRESH
,
293 .dir
= IIO_EV_DIR_FALLING
,
294 .mask_separate
= BIT(IIO_EV_INFO_VALUE
),
296 .type
= IIO_EV_TYPE_THRESH
,
297 .dir
= IIO_EV_DIR_EITHER
,
298 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
302 static int hi8435_get_sensing_mode(struct iio_dev
*idev
,
303 const struct iio_chan_spec
*chan
)
305 struct hi8435_priv
*priv
= iio_priv(idev
);
309 ret
= hi8435_readb(priv
, HI8435_PSEN_REG
, ®
);
313 return !!(reg
& BIT(chan
->channel
/ 8));
316 static int hi8435_set_sensing_mode(struct iio_dev
*idev
,
317 const struct iio_chan_spec
*chan
,
320 struct hi8435_priv
*priv
= iio_priv(idev
);
324 mutex_lock(&priv
->lock
);
326 ret
= hi8435_readb(priv
, HI8435_PSEN_REG
, ®
);
328 mutex_unlock(&priv
->lock
);
332 reg
&= ~BIT(chan
->channel
/ 8);
334 reg
|= BIT(chan
->channel
/ 8);
336 ret
= hi8435_writeb(priv
, HI8435_PSEN_REG
, reg
);
338 mutex_unlock(&priv
->lock
);
343 static const char * const hi8435_sensing_modes
[] = { "GND-Open",
346 static const struct iio_enum hi8435_sensing_mode
= {
347 .items
= hi8435_sensing_modes
,
348 .num_items
= ARRAY_SIZE(hi8435_sensing_modes
),
349 .get
= hi8435_get_sensing_mode
,
350 .set
= hi8435_set_sensing_mode
,
353 static const struct iio_chan_spec_ext_info hi8435_ext_info
[] = {
354 IIO_ENUM("sensing_mode", IIO_SEPARATE
, &hi8435_sensing_mode
),
355 IIO_ENUM_AVAILABLE("sensing_mode", &hi8435_sensing_mode
),
359 #define HI8435_VOLTAGE_CHANNEL(num) \
361 .type = IIO_VOLTAGE, \
364 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
365 .event_spec = hi8435_events, \
366 .num_event_specs = ARRAY_SIZE(hi8435_events), \
367 .ext_info = hi8435_ext_info, \
370 static const struct iio_chan_spec hi8435_channels
[] = {
371 HI8435_VOLTAGE_CHANNEL(0),
372 HI8435_VOLTAGE_CHANNEL(1),
373 HI8435_VOLTAGE_CHANNEL(2),
374 HI8435_VOLTAGE_CHANNEL(3),
375 HI8435_VOLTAGE_CHANNEL(4),
376 HI8435_VOLTAGE_CHANNEL(5),
377 HI8435_VOLTAGE_CHANNEL(6),
378 HI8435_VOLTAGE_CHANNEL(7),
379 HI8435_VOLTAGE_CHANNEL(8),
380 HI8435_VOLTAGE_CHANNEL(9),
381 HI8435_VOLTAGE_CHANNEL(10),
382 HI8435_VOLTAGE_CHANNEL(11),
383 HI8435_VOLTAGE_CHANNEL(12),
384 HI8435_VOLTAGE_CHANNEL(13),
385 HI8435_VOLTAGE_CHANNEL(14),
386 HI8435_VOLTAGE_CHANNEL(15),
387 HI8435_VOLTAGE_CHANNEL(16),
388 HI8435_VOLTAGE_CHANNEL(17),
389 HI8435_VOLTAGE_CHANNEL(18),
390 HI8435_VOLTAGE_CHANNEL(19),
391 HI8435_VOLTAGE_CHANNEL(20),
392 HI8435_VOLTAGE_CHANNEL(21),
393 HI8435_VOLTAGE_CHANNEL(22),
394 HI8435_VOLTAGE_CHANNEL(23),
395 HI8435_VOLTAGE_CHANNEL(24),
396 HI8435_VOLTAGE_CHANNEL(25),
397 HI8435_VOLTAGE_CHANNEL(26),
398 HI8435_VOLTAGE_CHANNEL(27),
399 HI8435_VOLTAGE_CHANNEL(28),
400 HI8435_VOLTAGE_CHANNEL(29),
401 HI8435_VOLTAGE_CHANNEL(30),
402 HI8435_VOLTAGE_CHANNEL(31),
403 IIO_CHAN_SOFT_TIMESTAMP(32),
406 static const struct iio_info hi8435_info
= {
407 .read_raw
= hi8435_read_raw
,
408 .read_event_config
= hi8435_read_event_config
,
409 .write_event_config
= hi8435_write_event_config
,
410 .read_event_value
= hi8435_read_event_value
,
411 .write_event_value
= hi8435_write_event_value
,
412 .debugfs_reg_access
= hi8435_debugfs_reg_access
,
415 static void hi8435_iio_push_event(struct iio_dev
*idev
, unsigned int val
)
417 struct hi8435_priv
*priv
= iio_priv(idev
);
418 enum iio_event_direction dir
;
420 unsigned int status
= priv
->event_prev_val
^ val
;
425 for_each_set_bit(i
, &priv
->event_scan_mask
, 32) {
426 if (status
& BIT(i
)) {
427 dir
= val
& BIT(i
) ? IIO_EV_DIR_RISING
:
430 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
, i
,
431 IIO_EV_TYPE_THRESH
, dir
),
432 iio_get_time_ns(idev
));
436 priv
->event_prev_val
= val
;
439 static irqreturn_t
hi8435_trigger_handler(int irq
, void *private)
441 struct iio_poll_func
*pf
= private;
442 struct iio_dev
*idev
= pf
->indio_dev
;
443 struct hi8435_priv
*priv
= iio_priv(idev
);
447 ret
= hi8435_readl(priv
, HI8435_SO31_0_REG
, &val
);
451 hi8435_iio_push_event(idev
, val
);
454 iio_trigger_notify_done(idev
->trig
);
459 static void hi8435_triggered_event_cleanup(void *data
)
461 iio_triggered_event_cleanup(data
);
464 static int hi8435_probe(struct spi_device
*spi
)
466 struct iio_dev
*idev
;
467 struct hi8435_priv
*priv
;
468 struct gpio_desc
*reset_gpio
;
471 idev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*priv
));
475 priv
= iio_priv(idev
);
478 reset_gpio
= devm_gpiod_get(&spi
->dev
, NULL
, GPIOD_OUT_LOW
);
479 if (IS_ERR(reset_gpio
)) {
480 /* chip s/w reset if h/w reset failed */
481 hi8435_writeb(priv
, HI8435_CTRL_REG
, HI8435_CTRL_SRST
);
482 hi8435_writeb(priv
, HI8435_CTRL_REG
, 0);
485 gpiod_set_value_cansleep(reset_gpio
, 1);
488 spi_set_drvdata(spi
, idev
);
489 mutex_init(&priv
->lock
);
491 idev
->dev
.parent
= &spi
->dev
;
492 idev
->dev
.of_node
= spi
->dev
.of_node
;
493 idev
->name
= spi_get_device_id(spi
)->name
;
494 idev
->modes
= INDIO_DIRECT_MODE
;
495 idev
->info
= &hi8435_info
;
496 idev
->channels
= hi8435_channels
;
497 idev
->num_channels
= ARRAY_SIZE(hi8435_channels
);
499 /* unmask all events */
500 priv
->event_scan_mask
= ~(0);
502 * There is a restriction in the chip - the hysteresis can not be odd.
503 * If the hysteresis is set to odd value then chip gets into lock state
504 * and not functional anymore.
505 * After chip reset the thresholds are in undefined state, so we need to
506 * initialize thresholds to some initial values and then prevent
507 * userspace setting odd hysteresis.
509 * Set threshold low voltage to 2V, threshold high voltage to 4V
510 * for both GND-Open and Supply-Open sensing modes.
512 priv
->threshold_lo
[0] = priv
->threshold_lo
[1] = 2;
513 priv
->threshold_hi
[0] = priv
->threshold_hi
[1] = 4;
514 hi8435_writew(priv
, HI8435_GOCENHYS_REG
, 0x206);
515 hi8435_writew(priv
, HI8435_SOCENHYS_REG
, 0x206);
517 ret
= iio_triggered_event_setup(idev
, NULL
, hi8435_trigger_handler
);
521 ret
= devm_add_action_or_reset(&spi
->dev
,
522 hi8435_triggered_event_cleanup
,
527 return devm_iio_device_register(&spi
->dev
, idev
);
530 static const struct of_device_id hi8435_dt_ids
[] = {
531 { .compatible
= "holt,hi8435" },
534 MODULE_DEVICE_TABLE(of
, hi8435_dt_ids
);
536 static const struct spi_device_id hi8435_id
[] = {
540 MODULE_DEVICE_TABLE(spi
, hi8435_id
);
542 static struct spi_driver hi8435_driver
= {
545 .of_match_table
= of_match_ptr(hi8435_dt_ids
),
547 .probe
= hi8435_probe
,
548 .id_table
= hi8435_id
,
550 module_spi_driver(hi8435_driver
);
552 MODULE_LICENSE("GPL");
553 MODULE_AUTHOR("Vladimir Barinov");
554 MODULE_DESCRIPTION("HI-8435 threshold detector");