Lines Matching +full:mode +full:- +full:reg
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Holt Integrated Circuits HI-8435 threshold detector driver
24 /* Register offsets for HI-8435 */
50 unsigned threshold_lo[2]; /* GND-Open and Supply-Open thresholds */
51 unsigned threshold_hi[2]; /* GND-Open and Supply-Open thresholds */
55 static int hi8435_readb(struct hi8435_priv *priv, u8 reg, u8 *val) in hi8435_readb() argument
57 reg |= HI8435_READ_OPCODE; in hi8435_readb()
58 return spi_write_then_read(priv->spi, ®, 1, val, 1); in hi8435_readb()
61 static int hi8435_readw(struct hi8435_priv *priv, u8 reg, u16 *val) in hi8435_readw() argument
66 reg |= HI8435_READ_OPCODE; in hi8435_readw()
67 ret = spi_write_then_read(priv->spi, ®, 1, &be_val, 2); in hi8435_readw()
73 static int hi8435_readl(struct hi8435_priv *priv, u8 reg, u32 *val) in hi8435_readl() argument
78 reg |= HI8435_READ_OPCODE; in hi8435_readl()
79 ret = spi_write_then_read(priv->spi, ®, 1, &be_val, 4); in hi8435_readl()
85 static int hi8435_writeb(struct hi8435_priv *priv, u8 reg, u8 val) in hi8435_writeb() argument
87 priv->reg_buffer[0] = reg | HI8435_WRITE_OPCODE; in hi8435_writeb()
88 priv->reg_buffer[1] = val; in hi8435_writeb()
90 return spi_write(priv->spi, priv->reg_buffer, 2); in hi8435_writeb()
93 static int hi8435_writew(struct hi8435_priv *priv, u8 reg, u16 val) in hi8435_writew() argument
95 priv->reg_buffer[0] = reg | HI8435_WRITE_OPCODE; in hi8435_writew()
96 priv->reg_buffer[1] = (val >> 8) & 0xff; in hi8435_writew()
97 priv->reg_buffer[2] = val & 0xff; in hi8435_writew()
99 return spi_write(priv->spi, priv->reg_buffer, 3); in hi8435_writew()
115 *val = !!(tmp & BIT(chan->channel)); in hi8435_read_raw()
118 return -EINVAL; in hi8435_read_raw()
129 return !!(priv->event_scan_mask & BIT(chan->channel)); in hi8435_read_event_config()
145 if (tmp & BIT(chan->channel)) in hi8435_write_event_config()
146 priv->event_prev_val |= BIT(chan->channel); in hi8435_write_event_config()
148 priv->event_prev_val &= ~BIT(chan->channel); in hi8435_write_event_config()
150 priv->event_scan_mask |= BIT(chan->channel); in hi8435_write_event_config()
152 priv->event_scan_mask &= ~BIT(chan->channel); in hi8435_write_event_config()
166 u8 mode, psen; in hi8435_read_event_value() local
167 u16 reg; in hi8435_read_event_value() local
173 /* Supply-Open or GND-Open sensing mode */ in hi8435_read_event_value()
174 mode = !!(psen & BIT(chan->channel / 8)); in hi8435_read_event_value()
176 ret = hi8435_readw(priv, mode ? HI8435_SOCENHYS_REG : in hi8435_read_event_value()
177 HI8435_GOCENHYS_REG, ®); in hi8435_read_event_value()
182 *val = ((reg & 0xff) - (reg >> 8)) / 2; in hi8435_read_event_value()
184 *val = ((reg & 0xff) + (reg >> 8)) / 2; in hi8435_read_event_value()
198 u8 mode, psen; in hi8435_write_event_value() local
199 u16 reg; in hi8435_write_event_value() local
205 /* Supply-Open or GND-Open sensing mode */ in hi8435_write_event_value()
206 mode = !!(psen & BIT(chan->channel / 8)); in hi8435_write_event_value()
208 ret = hi8435_readw(priv, mode ? HI8435_SOCENHYS_REG : in hi8435_write_event_value()
209 HI8435_GOCENHYS_REG, ®); in hi8435_write_event_value()
215 if (val < 2 || val > 21 || (val + 2) > priv->threshold_hi[mode]) in hi8435_write_event_value()
216 return -EINVAL; in hi8435_write_event_value()
218 if (val == priv->threshold_lo[mode]) in hi8435_write_event_value()
221 priv->threshold_lo[mode] = val; in hi8435_write_event_value()
224 if ((priv->threshold_hi[mode] - priv->threshold_lo[mode]) % 2) in hi8435_write_event_value()
225 priv->threshold_hi[mode]--; in hi8435_write_event_value()
228 if (val < 3 || val > 22 || val < (priv->threshold_lo[mode] + 2)) in hi8435_write_event_value()
229 return -EINVAL; in hi8435_write_event_value()
231 if (val == priv->threshold_hi[mode]) in hi8435_write_event_value()
234 priv->threshold_hi[mode] = val; in hi8435_write_event_value()
237 if ((priv->threshold_hi[mode] - priv->threshold_lo[mode]) % 2) in hi8435_write_event_value()
238 priv->threshold_lo[mode]++; in hi8435_write_event_value()
242 mutex_lock(&priv->lock); in hi8435_write_event_value()
244 ret = hi8435_readw(priv, mode ? HI8435_SOCENHYS_REG : in hi8435_write_event_value()
245 HI8435_GOCENHYS_REG, ®); in hi8435_write_event_value()
247 mutex_unlock(&priv->lock); in hi8435_write_event_value()
252 reg = priv->threshold_hi[mode] - priv->threshold_lo[mode]; in hi8435_write_event_value()
253 reg <<= 8; in hi8435_write_event_value()
255 reg |= (priv->threshold_hi[mode] + priv->threshold_lo[mode]); in hi8435_write_event_value()
257 ret = hi8435_writew(priv, mode ? HI8435_SOCENHYS_REG : in hi8435_write_event_value()
258 HI8435_GOCENHYS_REG, reg); in hi8435_write_event_value()
260 mutex_unlock(&priv->lock); in hi8435_write_event_value()
266 unsigned reg, unsigned writeval, in hi8435_debugfs_reg_access() argument
274 ret = hi8435_readb(priv, reg, &val); in hi8435_debugfs_reg_access()
278 ret = hi8435_writeb(priv, reg, val); in hi8435_debugfs_reg_access()
305 u8 reg; in hi8435_get_sensing_mode() local
307 ret = hi8435_readb(priv, HI8435_PSEN_REG, ®); in hi8435_get_sensing_mode()
311 return !!(reg & BIT(chan->channel / 8)); in hi8435_get_sensing_mode()
316 unsigned int mode) in hi8435_set_sensing_mode() argument
320 u8 reg; in hi8435_set_sensing_mode() local
322 mutex_lock(&priv->lock); in hi8435_set_sensing_mode()
324 ret = hi8435_readb(priv, HI8435_PSEN_REG, ®); in hi8435_set_sensing_mode()
326 mutex_unlock(&priv->lock); in hi8435_set_sensing_mode()
330 reg &= ~BIT(chan->channel / 8); in hi8435_set_sensing_mode()
331 if (mode) in hi8435_set_sensing_mode()
332 reg |= BIT(chan->channel / 8); in hi8435_set_sensing_mode()
334 ret = hi8435_writeb(priv, HI8435_PSEN_REG, reg); in hi8435_set_sensing_mode()
336 mutex_unlock(&priv->lock); in hi8435_set_sensing_mode()
341 static const char * const hi8435_sensing_modes[] = { "GND-Open",
342 "Supply-Open" };
418 unsigned int status = priv->event_prev_val ^ val; in hi8435_iio_push_event()
423 for_each_set_bit(i, &priv->event_scan_mask, 32) { in hi8435_iio_push_event()
434 priv->event_prev_val = val; in hi8435_iio_push_event()
440 struct iio_dev *idev = pf->indio_dev; in hi8435_trigger_handler()
452 iio_trigger_notify_done(idev->trig); in hi8435_trigger_handler()
469 idev = devm_iio_device_alloc(&spi->dev, sizeof(*priv)); in hi8435_probe()
471 return -ENOMEM; in hi8435_probe()
474 priv->spi = spi; in hi8435_probe()
476 reset_gpio = devm_gpiod_get(&spi->dev, NULL, GPIOD_OUT_LOW); in hi8435_probe()
486 mutex_init(&priv->lock); in hi8435_probe()
488 idev->name = spi_get_device_id(spi)->name; in hi8435_probe()
489 idev->modes = INDIO_DIRECT_MODE; in hi8435_probe()
490 idev->info = &hi8435_info; in hi8435_probe()
491 idev->channels = hi8435_channels; in hi8435_probe()
492 idev->num_channels = ARRAY_SIZE(hi8435_channels); in hi8435_probe()
495 priv->event_scan_mask = ~(0); in hi8435_probe()
497 * There is a restriction in the chip - the hysteresis can not be odd. in hi8435_probe()
505 * for both GND-Open and Supply-Open sensing modes. in hi8435_probe()
507 priv->threshold_lo[0] = priv->threshold_lo[1] = 2; in hi8435_probe()
508 priv->threshold_hi[0] = priv->threshold_hi[1] = 4; in hi8435_probe()
516 ret = devm_add_action_or_reset(&spi->dev, in hi8435_probe()
522 return devm_iio_device_register(&spi->dev, idev); in hi8435_probe()
549 MODULE_DESCRIPTION("HI-8435 threshold detector");