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
22 /* Register offsets for HI-8435 */
48 unsigned threshold_lo[2]; /* GND-Open and Supply-Open thresholds */
49 unsigned threshold_hi[2]; /* GND-Open and Supply-Open thresholds */
53 static int hi8435_readb(struct hi8435_priv *priv, u8 reg, u8 *val) in hi8435_readb() argument
55 reg |= HI8435_READ_OPCODE; in hi8435_readb()
56 return spi_write_then_read(priv->spi, &reg, 1, val, 1); in hi8435_readb()
59 static int hi8435_readw(struct hi8435_priv *priv, u8 reg, u16 *val) in hi8435_readw() argument
64 reg |= HI8435_READ_OPCODE; in hi8435_readw()
65 ret = spi_write_then_read(priv->spi, &reg, 1, &be_val, 2); in hi8435_readw()
71 static int hi8435_readl(struct hi8435_priv *priv, u8 reg, u32 *val) in hi8435_readl() argument
76 reg |= HI8435_READ_OPCODE; in hi8435_readl()
77 ret = spi_write_then_read(priv->spi, &reg, 1, &be_val, 4); in hi8435_readl()
83 static int hi8435_writeb(struct hi8435_priv *priv, u8 reg, u8 val) in hi8435_writeb() argument
85 priv->reg_buffer[0] = reg | HI8435_WRITE_OPCODE; in hi8435_writeb()
86 priv->reg_buffer[1] = val; in hi8435_writeb()
88 return spi_write(priv->spi, priv->reg_buffer, 2); in hi8435_writeb()
91 static int hi8435_writew(struct hi8435_priv *priv, u8 reg, u16 val) in hi8435_writew() argument
93 priv->reg_buffer[0] = reg | HI8435_WRITE_OPCODE; in hi8435_writew()
94 priv->reg_buffer[1] = (val >> 8) & 0xff; in hi8435_writew()
95 priv->reg_buffer[2] = val & 0xff; in hi8435_writew()
97 return spi_write(priv->spi, priv->reg_buffer, 3); in hi8435_writew()
113 *val = !!(tmp & BIT(chan->channel)); in hi8435_read_raw()
116 return -EINVAL; in hi8435_read_raw()
127 return !!(priv->event_scan_mask & BIT(chan->channel)); in hi8435_read_event_config()
143 if (tmp & BIT(chan->channel)) in hi8435_write_event_config()
144 priv->event_prev_val |= BIT(chan->channel); in hi8435_write_event_config()
146 priv->event_prev_val &= ~BIT(chan->channel); in hi8435_write_event_config()
148 priv->event_scan_mask |= BIT(chan->channel); in hi8435_write_event_config()
150 priv->event_scan_mask &= ~BIT(chan->channel); in hi8435_write_event_config()
164 u8 mode, psen; in hi8435_read_event_value() local
165 u16 reg; in hi8435_read_event_value() local
171 /* Supply-Open or GND-Open sensing mode */ in hi8435_read_event_value()
172 mode = !!(psen & BIT(chan->channel / 8)); in hi8435_read_event_value()
174 ret = hi8435_readw(priv, mode ? HI8435_SOCENHYS_REG : in hi8435_read_event_value()
175 HI8435_GOCENHYS_REG, &reg); in hi8435_read_event_value()
180 *val = ((reg & 0xff) - (reg >> 8)) / 2; in hi8435_read_event_value()
182 *val = ((reg & 0xff) + (reg >> 8)) / 2; in hi8435_read_event_value()
196 u8 mode, psen; in hi8435_write_event_value() local
197 u16 reg; in hi8435_write_event_value() local
203 /* Supply-Open or GND-Open sensing mode */ in hi8435_write_event_value()
204 mode = !!(psen & BIT(chan->channel / 8)); in hi8435_write_event_value()
206 ret = hi8435_readw(priv, mode ? HI8435_SOCENHYS_REG : in hi8435_write_event_value()
207 HI8435_GOCENHYS_REG, &reg); in hi8435_write_event_value()
213 if (val < 2 || val > 21 || (val + 2) > priv->threshold_hi[mode]) in hi8435_write_event_value()
214 return -EINVAL; in hi8435_write_event_value()
216 if (val == priv->threshold_lo[mode]) in hi8435_write_event_value()
219 priv->threshold_lo[mode] = val; in hi8435_write_event_value()
222 if ((priv->threshold_hi[mode] - priv->threshold_lo[mode]) % 2) in hi8435_write_event_value()
223 priv->threshold_hi[mode]--; in hi8435_write_event_value()
226 if (val < 3 || val > 22 || val < (priv->threshold_lo[mode] + 2)) in hi8435_write_event_value()
227 return -EINVAL; in hi8435_write_event_value()
229 if (val == priv->threshold_hi[mode]) in hi8435_write_event_value()
232 priv->threshold_hi[mode] = val; in hi8435_write_event_value()
235 if ((priv->threshold_hi[mode] - priv->threshold_lo[mode]) % 2) in hi8435_write_event_value()
236 priv->threshold_lo[mode]++; in hi8435_write_event_value()
240 mutex_lock(&priv->lock); in hi8435_write_event_value()
242 ret = hi8435_readw(priv, mode ? HI8435_SOCENHYS_REG : in hi8435_write_event_value()
243 HI8435_GOCENHYS_REG, &reg); in hi8435_write_event_value()
245 mutex_unlock(&priv->lock); in hi8435_write_event_value()
250 reg = priv->threshold_hi[mode] - priv->threshold_lo[mode]; in hi8435_write_event_value()
251 reg <<= 8; in hi8435_write_event_value()
253 reg |= (priv->threshold_hi[mode] + priv->threshold_lo[mode]); in hi8435_write_event_value()
255 ret = hi8435_writew(priv, mode ? HI8435_SOCENHYS_REG : in hi8435_write_event_value()
256 HI8435_GOCENHYS_REG, reg); in hi8435_write_event_value()
258 mutex_unlock(&priv->lock); in hi8435_write_event_value()
264 unsigned reg, unsigned writeval, in hi8435_debugfs_reg_access() argument
272 ret = hi8435_readb(priv, reg, &val); in hi8435_debugfs_reg_access()
276 ret = hi8435_writeb(priv, reg, val); in hi8435_debugfs_reg_access()
303 u8 reg; in hi8435_get_sensing_mode() local
305 ret = hi8435_readb(priv, HI8435_PSEN_REG, &reg); in hi8435_get_sensing_mode()
309 return !!(reg & BIT(chan->channel / 8)); in hi8435_get_sensing_mode()
314 unsigned int mode) in hi8435_set_sensing_mode() argument
318 u8 reg; in hi8435_set_sensing_mode() local
320 mutex_lock(&priv->lock); in hi8435_set_sensing_mode()
322 ret = hi8435_readb(priv, HI8435_PSEN_REG, &reg); in hi8435_set_sensing_mode()
324 mutex_unlock(&priv->lock); in hi8435_set_sensing_mode()
328 reg &= ~BIT(chan->channel / 8); in hi8435_set_sensing_mode()
329 if (mode) in hi8435_set_sensing_mode()
330 reg |= BIT(chan->channel / 8); in hi8435_set_sensing_mode()
332 ret = hi8435_writeb(priv, HI8435_PSEN_REG, reg); in hi8435_set_sensing_mode()
334 mutex_unlock(&priv->lock); in hi8435_set_sensing_mode()
339 static const char * const hi8435_sensing_modes[] = { "GND-Open",
340 "Supply-Open" };
416 unsigned int status = priv->event_prev_val ^ val; in hi8435_iio_push_event()
421 for_each_set_bit(i, &priv->event_scan_mask, 32) { in hi8435_iio_push_event()
432 priv->event_prev_val = val; in hi8435_iio_push_event()
438 struct iio_dev *idev = pf->indio_dev; in hi8435_trigger_handler()
450 iio_trigger_notify_done(idev->trig); in hi8435_trigger_handler()
467 idev = devm_iio_device_alloc(&spi->dev, sizeof(*priv)); in hi8435_probe()
469 return -ENOMEM; in hi8435_probe()
472 priv->spi = spi; in hi8435_probe()
474 reset_gpio = devm_gpiod_get(&spi->dev, NULL, GPIOD_OUT_LOW); in hi8435_probe()
484 mutex_init(&priv->lock); in hi8435_probe()
486 idev->name = spi_get_device_id(spi)->name; in hi8435_probe()
487 idev->modes = INDIO_DIRECT_MODE; in hi8435_probe()
488 idev->info = &hi8435_info; in hi8435_probe()
489 idev->channels = hi8435_channels; in hi8435_probe()
490 idev->num_channels = ARRAY_SIZE(hi8435_channels); in hi8435_probe()
493 priv->event_scan_mask = ~(0); in hi8435_probe()
495 * There is a restriction in the chip - the hysteresis can not be odd. in hi8435_probe()
503 * for both GND-Open and Supply-Open sensing modes. in hi8435_probe()
505 priv->threshold_lo[0] = priv->threshold_lo[1] = 2; in hi8435_probe()
506 priv->threshold_hi[0] = priv->threshold_hi[1] = 4; in hi8435_probe()
514 ret = devm_add_action_or_reset(&spi->dev, in hi8435_probe()
520 return devm_iio_device_register(&spi->dev, idev); in hi8435_probe()
547 MODULE_DESCRIPTION("HI-8435 threshold detector");