1 // SPDX-License-Identifier: GPL-2.0
3 * Texas Instruments TSC2046 SPI ADC driver
8 #include <linux/bitfield.h>
9 #include <linux/delay.h>
10 #include <linux/module.h>
11 #include <linux/regulator/consumer.h>
12 #include <linux/spi/spi.h>
13 #include <linux/units.h>
15 #include <asm/unaligned.h>
17 #include <linux/iio/buffer.h>
18 #include <linux/iio/trigger_consumer.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include <linux/iio/trigger.h>
23 * The PENIRQ of TSC2046 controller is implemented as level shifter attached to
24 * the X+ line. If voltage of the X+ line reaches a specific level the IRQ will
25 * be activated or deactivated.
26 * To make this kind of IRQ reusable as trigger following additions were
29 * For typical touchscreen use case, we need to trigger about each 10ms.
31 * Continue triggering at least once after the IRQ was deactivated. Then
32 * deactivate this trigger to stop sampling in order to reduce power
36 #define TI_TSC2046_NAME "tsc2046"
38 /* This driver doesn't aim at the peak continuous sample rate */
39 #define TI_TSC2046_MAX_SAMPLE_RATE 125000
40 #define TI_TSC2046_SAMPLE_BITS \
41 BITS_PER_TYPE(struct tsc2046_adc_atom)
42 #define TI_TSC2046_MAX_CLK_FREQ \
43 (TI_TSC2046_MAX_SAMPLE_RATE * TI_TSC2046_SAMPLE_BITS)
45 #define TI_TSC2046_SAMPLE_INTERVAL_US 10000
47 #define TI_TSC2046_START BIT(7)
48 #define TI_TSC2046_ADDR GENMASK(6, 4)
49 #define TI_TSC2046_ADDR_TEMP1 7
50 #define TI_TSC2046_ADDR_AUX 6
51 #define TI_TSC2046_ADDR_X 5
52 #define TI_TSC2046_ADDR_Z2 4
53 #define TI_TSC2046_ADDR_Z1 3
54 #define TI_TSC2046_ADDR_VBAT 2
55 #define TI_TSC2046_ADDR_Y 1
56 #define TI_TSC2046_ADDR_TEMP0 0
59 * The mode bit sets the resolution of the ADC. With this bit low, the next
60 * conversion has 12-bit resolution, whereas with this bit high, the next
61 * conversion has 8-bit resolution. This driver is optimized for 12-bit mode.
62 * So, for this driver, this bit should stay zero.
64 #define TI_TSC2046_8BIT_MODE BIT(3)
67 * SER/DFR - The SER/DFR bit controls the reference mode, either single-ended
68 * (high) or differential (low).
70 #define TI_TSC2046_SER BIT(2)
73 * If VREF_ON and ADC_ON are both zero, then the chip operates in
74 * auto-wake/suspend mode. In most case this bits should stay zero.
76 #define TI_TSC2046_PD1_VREF_ON BIT(1)
77 #define TI_TSC2046_PD0_ADC_ON BIT(0)
80 * All supported devices can do 8 or 12bit resolution. This driver
81 * supports only 12bit mode, here we have a 16bit data transfer, where
82 * the MSB and the 3 LSB are 0.
84 #define TI_TSC2046_DATA_12BIT GENMASK(14, 3)
86 #define TI_TSC2046_MAX_CHAN 8
87 #define TI_TSC2046_MIN_POLL_CNT 3
88 #define TI_TSC2046_EXT_POLL_CNT 3
89 #define TI_TSC2046_POLL_CNT \
90 (TI_TSC2046_MIN_POLL_CNT + TI_TSC2046_EXT_POLL_CNT)
91 #define TI_TSC2046_INT_VREF 2500
93 /* Represents a HW sample */
94 struct tsc2046_adc_atom {
96 * Command transmitted to the controller. This field is empty on the RX
101 * Data received from the controller. This field is empty for the TX
107 /* Layout of atomic buffers within big buffer */
108 struct tsc2046_adc_group_layout {
109 /* Group offset within the SPI RX buffer */
112 * Amount of tsc2046_adc_atom structs within the same command gathered
117 * Settling samples (tsc2046_adc_atom structs) which should be skipped
118 * before good samples will start.
123 struct tsc2046_adc_dcfg {
124 const struct iio_chan_spec *channels;
125 unsigned int num_channels;
128 struct tsc2046_adc_ch_cfg {
129 unsigned int settling_time_us;
130 unsigned int oversampling_ratio;
134 TSC2046_STATE_SHUTDOWN,
135 TSC2046_STATE_STANDBY,
137 TSC2046_STATE_POLL_IRQ_DISABLE,
138 TSC2046_STATE_ENABLE_IRQ,
141 struct tsc2046_adc_priv {
142 struct spi_device *spi;
143 const struct tsc2046_adc_dcfg *dcfg;
144 struct regulator *vref_reg;
146 struct iio_trigger *trig;
147 struct hrtimer trig_timer;
148 enum tsc2046_state state;
150 spinlock_t state_lock;
152 struct spi_transfer xfer;
153 struct spi_message msg;
156 /* Scan data for each channel */
157 u16 data[TI_TSC2046_MAX_CHAN];
163 * Lock to protect the layout and the SPI transfer buffer.
164 * tsc2046_adc_group_layout can be changed within update_scan_mode(),
165 * in this case the l[] and tx/rx buffer will be out of sync to each
169 struct tsc2046_adc_group_layout l[TI_TSC2046_MAX_CHAN];
170 struct tsc2046_adc_atom *rx;
171 struct tsc2046_adc_atom *tx;
175 u32 effective_speed_hz;
176 u32 scan_interval_us;
177 u32 time_per_scan_us;
179 unsigned int vref_mv;
181 struct tsc2046_adc_ch_cfg ch_cfg[TI_TSC2046_MAX_CHAN];
184 #define TI_TSC2046_V_CHAN(index, bits, name) \
186 .type = IIO_VOLTAGE, \
189 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
190 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
191 .datasheet_name = "#name", \
192 .scan_index = index, \
197 .endianness = IIO_CPU, \
201 #define DECLARE_TI_TSC2046_8_CHANNELS(name, bits) \
202 const struct iio_chan_spec name ## _channels[] = { \
203 TI_TSC2046_V_CHAN(0, bits, TEMP0), \
204 TI_TSC2046_V_CHAN(1, bits, Y), \
205 TI_TSC2046_V_CHAN(2, bits, VBAT), \
206 TI_TSC2046_V_CHAN(3, bits, Z1), \
207 TI_TSC2046_V_CHAN(4, bits, Z2), \
208 TI_TSC2046_V_CHAN(5, bits, X), \
209 TI_TSC2046_V_CHAN(6, bits, AUX), \
210 TI_TSC2046_V_CHAN(7, bits, TEMP1), \
211 IIO_CHAN_SOFT_TIMESTAMP(8), \
214 static DECLARE_TI_TSC2046_8_CHANNELS(tsc2046_adc, 12);
216 static const struct tsc2046_adc_dcfg tsc2046_adc_dcfg_tsc2046e = {
217 .channels = tsc2046_adc_channels,
218 .num_channels = ARRAY_SIZE(tsc2046_adc_channels),
222 * Convert time to a number of samples which can be transferred within this
225 static unsigned int tsc2046_adc_time_to_count(struct tsc2046_adc_priv *priv,
228 unsigned int bit_count, sample_count;
230 bit_count = DIV_ROUND_UP(time * NSEC_PER_USEC, priv->time_per_bit_ns);
231 sample_count = DIV_ROUND_UP(bit_count, TI_TSC2046_SAMPLE_BITS);
233 dev_dbg(&priv->spi->dev, "Effective speed %u, time per bit: %u, count bits: %u, count samples: %u\n",
234 priv->effective_speed_hz, priv->time_per_bit_ns,
235 bit_count, sample_count);
240 static u8 tsc2046_adc_get_cmd(struct tsc2046_adc_priv *priv, int ch_idx,
246 * if PD bits are 0, controller will automatically disable ADC, VREF and
250 pd = TI_TSC2046_PD0_ADC_ON;
255 case TI_TSC2046_ADDR_TEMP1:
256 case TI_TSC2046_ADDR_AUX:
257 case TI_TSC2046_ADDR_VBAT:
258 case TI_TSC2046_ADDR_TEMP0:
259 pd |= TI_TSC2046_SER;
261 pd |= TI_TSC2046_PD1_VREF_ON;
264 return TI_TSC2046_START | FIELD_PREP(TI_TSC2046_ADDR, ch_idx) | pd;
267 static u16 tsc2046_adc_get_value(struct tsc2046_adc_atom *buf)
269 return FIELD_GET(TI_TSC2046_DATA_12BIT, get_unaligned_be16(&buf->data));
272 static int tsc2046_adc_read_one(struct tsc2046_adc_priv *priv, int ch_idx,
273 u32 *effective_speed_hz)
275 struct tsc2046_adc_ch_cfg *ch = &priv->ch_cfg[ch_idx];
276 struct tsc2046_adc_atom *rx_buf, *tx_buf;
277 unsigned int val, val_normalized = 0;
278 int ret, i, count_skip = 0, max_count;
279 struct spi_transfer xfer;
280 struct spi_message msg;
283 if (!effective_speed_hz) {
284 count_skip = tsc2046_adc_time_to_count(priv, ch->settling_time_us);
285 max_count = count_skip + ch->oversampling_ratio;
290 if (sizeof(*tx_buf) * max_count > PAGE_SIZE)
293 tx_buf = kcalloc(max_count, sizeof(*tx_buf), GFP_KERNEL);
297 rx_buf = kcalloc(max_count, sizeof(*rx_buf), GFP_KERNEL);
304 * Do not enable automatic power down on working samples. Otherwise the
305 * plates will never be completely charged.
307 cmd = tsc2046_adc_get_cmd(priv, ch_idx, true);
309 for (i = 0; i < max_count - 1; i++)
312 /* automatically power down on last sample */
313 tx_buf[i].cmd = tsc2046_adc_get_cmd(priv, ch_idx, false);
315 memset(&xfer, 0, sizeof(xfer));
316 xfer.tx_buf = tx_buf;
317 xfer.rx_buf = rx_buf;
318 xfer.len = sizeof(*tx_buf) * max_count;
319 spi_message_init_with_transfers(&msg, &xfer, 1);
322 * We aren't using spi_write_then_read() because we need to be able
323 * to get hold of the effective_speed_hz from the xfer
325 ret = spi_sync(priv->spi, &msg);
327 dev_err_ratelimited(&priv->spi->dev, "SPI transfer failed %pe\n",
332 if (effective_speed_hz)
333 *effective_speed_hz = xfer.effective_speed_hz;
335 for (i = 0; i < max_count - count_skip; i++) {
336 val = tsc2046_adc_get_value(&rx_buf[count_skip + i]);
337 val_normalized += val;
340 ret = DIV_ROUND_UP(val_normalized, max_count - count_skip);
350 static size_t tsc2046_adc_group_set_layout(struct tsc2046_adc_priv *priv,
354 struct tsc2046_adc_ch_cfg *ch = &priv->ch_cfg[ch_idx];
355 struct tsc2046_adc_group_layout *cur;
356 unsigned int max_count, count_skip;
357 unsigned int offset = 0;
360 offset = priv->l[group - 1].offset + priv->l[group - 1].count;
362 count_skip = tsc2046_adc_time_to_count(priv, ch->settling_time_us);
363 max_count = count_skip + ch->oversampling_ratio;
365 cur = &priv->l[group];
366 cur->offset = offset;
367 cur->count = max_count;
368 cur->skip = count_skip;
370 return sizeof(*priv->tx) * max_count;
373 static void tsc2046_adc_group_set_cmd(struct tsc2046_adc_priv *priv,
374 unsigned int group, int ch_idx)
376 struct tsc2046_adc_group_layout *l = &priv->l[group];
381 * Do not enable automatic power down on working samples. Otherwise the
382 * plates will never be completely charged.
384 cmd = tsc2046_adc_get_cmd(priv, ch_idx, true);
386 for (i = 0; i < l->count - 1; i++)
387 priv->tx[l->offset + i].cmd = cmd;
389 /* automatically power down on last sample */
390 priv->tx[l->offset + i].cmd = tsc2046_adc_get_cmd(priv, ch_idx, false);
393 static u16 tsc2046_adc_get_val(struct tsc2046_adc_priv *priv, int group)
395 struct tsc2046_adc_group_layout *l;
396 unsigned int val, val_normalized = 0;
400 valid_count = l->count - l->skip;
402 for (i = 0; i < valid_count; i++) {
403 val = tsc2046_adc_get_value(&priv->rx[l->offset + l->skip + i]);
404 val_normalized += val;
407 return DIV_ROUND_UP(val_normalized, valid_count);
410 static int tsc2046_adc_scan(struct iio_dev *indio_dev)
412 struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
413 struct device *dev = &priv->spi->dev;
417 ret = spi_sync(priv->spi, &priv->msg);
419 dev_err_ratelimited(dev, "SPI transfer failed: %pe\n", ERR_PTR(ret));
423 for (group = 0; group < priv->groups; group++)
424 priv->scan_buf.data[group] = tsc2046_adc_get_val(priv, group);
426 ret = iio_push_to_buffers_with_timestamp(indio_dev, &priv->scan_buf,
427 iio_get_time_ns(indio_dev));
428 /* If the consumer is kfifo, we may get a EBUSY here - ignore it. */
429 if (ret < 0 && ret != -EBUSY) {
430 dev_err_ratelimited(dev, "Failed to push scan buffer %pe\n",
439 static irqreturn_t tsc2046_adc_trigger_handler(int irq, void *p)
441 struct iio_poll_func *pf = p;
442 struct iio_dev *indio_dev = pf->indio_dev;
443 struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
445 mutex_lock(&priv->slock);
446 tsc2046_adc_scan(indio_dev);
447 mutex_unlock(&priv->slock);
449 iio_trigger_notify_done(indio_dev->trig);
454 static int tsc2046_adc_read_raw(struct iio_dev *indio_dev,
455 struct iio_chan_spec const *chan,
456 int *val, int *val2, long m)
458 struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
462 case IIO_CHAN_INFO_RAW:
463 ret = tsc2046_adc_read_one(priv, chan->channel, NULL);
470 case IIO_CHAN_INFO_SCALE:
472 * Note: the TSC2046 has internal voltage divider on the VBAT
473 * line. This divider can be influenced by external divider.
474 * So, it is better to use external voltage-divider driver
475 * instead, which is calculating complete chain.
477 *val = priv->vref_mv;
478 *val2 = chan->scan_type.realbits;
479 return IIO_VAL_FRACTIONAL_LOG2;
485 static int tsc2046_adc_update_scan_mode(struct iio_dev *indio_dev,
486 const unsigned long *active_scan_mask)
488 struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
489 unsigned int ch_idx, group = 0;
492 mutex_lock(&priv->slock);
495 for_each_set_bit(ch_idx, active_scan_mask, ARRAY_SIZE(priv->l)) {
496 size += tsc2046_adc_group_set_layout(priv, group, ch_idx);
497 tsc2046_adc_group_set_cmd(priv, group, ch_idx);
501 priv->groups = group;
502 priv->xfer.len = size;
503 priv->time_per_scan_us = size * 8 * priv->time_per_bit_ns / NSEC_PER_USEC;
505 if (priv->scan_interval_us < priv->time_per_scan_us)
506 dev_warn(&priv->spi->dev, "The scan interval (%d) is less then calculated scan time (%d)\n",
507 priv->scan_interval_us, priv->time_per_scan_us);
509 mutex_unlock(&priv->slock);
514 static const struct iio_info tsc2046_adc_info = {
515 .read_raw = tsc2046_adc_read_raw,
516 .update_scan_mode = tsc2046_adc_update_scan_mode,
519 static enum hrtimer_restart tsc2046_adc_timer(struct hrtimer *hrtimer)
521 struct tsc2046_adc_priv *priv = container_of(hrtimer,
522 struct tsc2046_adc_priv,
527 * This state machine should address following challenges :
528 * - the interrupt source is based on level shifter attached to the X
529 * channel of ADC. It will change the state every time we switch
530 * between channels. So, we need to disable IRQ if we do
531 * iio_trigger_poll().
532 * - we should do iio_trigger_poll() at some reduced sample rate
533 * - we should still trigger for some amount of time after last
534 * interrupt with enabled IRQ was processed.
537 spin_lock_irqsave(&priv->state_lock, flags);
538 switch (priv->state) {
539 case TSC2046_STATE_ENABLE_IRQ:
540 if (priv->poll_cnt < TI_TSC2046_POLL_CNT) {
542 hrtimer_start(&priv->trig_timer,
543 ns_to_ktime(priv->scan_interval_us *
545 HRTIMER_MODE_REL_SOFT);
547 if (priv->poll_cnt >= TI_TSC2046_MIN_POLL_CNT) {
548 priv->state = TSC2046_STATE_POLL_IRQ_DISABLE;
549 enable_irq(priv->spi->irq);
551 priv->state = TSC2046_STATE_POLL;
554 priv->state = TSC2046_STATE_STANDBY;
555 enable_irq(priv->spi->irq);
558 case TSC2046_STATE_POLL_IRQ_DISABLE:
559 disable_irq_nosync(priv->spi->irq);
561 case TSC2046_STATE_POLL:
562 priv->state = TSC2046_STATE_ENABLE_IRQ;
563 /* iio_trigger_poll() starts hrtimer */
564 iio_trigger_poll(priv->trig);
566 case TSC2046_STATE_SHUTDOWN:
568 case TSC2046_STATE_STANDBY:
571 dev_warn(&priv->spi->dev, "Got unexpected state: %i\n",
575 spin_unlock_irqrestore(&priv->state_lock, flags);
577 return HRTIMER_NORESTART;
580 static irqreturn_t tsc2046_adc_irq(int irq, void *dev_id)
582 struct iio_dev *indio_dev = dev_id;
583 struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
586 hrtimer_try_to_cancel(&priv->trig_timer);
588 spin_lock_irqsave(&priv->state_lock, flags);
589 if (priv->state != TSC2046_STATE_SHUTDOWN) {
590 priv->state = TSC2046_STATE_ENABLE_IRQ;
593 /* iio_trigger_poll() starts hrtimer */
594 disable_irq_nosync(priv->spi->irq);
595 iio_trigger_poll(priv->trig);
597 spin_unlock_irqrestore(&priv->state_lock, flags);
602 static void tsc2046_adc_reenable_trigger(struct iio_trigger *trig)
604 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
605 struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
609 * We can sample it as fast as we can, but usually we do not need so
610 * many samples. Reduce the sample rate for default (touchscreen) use
613 tim = ns_to_ktime((priv->scan_interval_us - priv->time_per_scan_us) *
615 hrtimer_start(&priv->trig_timer, tim, HRTIMER_MODE_REL_SOFT);
618 static int tsc2046_adc_set_trigger_state(struct iio_trigger *trig, bool enable)
620 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
621 struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
625 spin_lock_irqsave(&priv->state_lock, flags);
626 if (priv->state == TSC2046_STATE_SHUTDOWN) {
627 priv->state = TSC2046_STATE_STANDBY;
628 enable_irq(priv->spi->irq);
630 spin_unlock_irqrestore(&priv->state_lock, flags);
632 spin_lock_irqsave(&priv->state_lock, flags);
634 if (priv->state == TSC2046_STATE_STANDBY ||
635 priv->state == TSC2046_STATE_POLL_IRQ_DISABLE)
636 disable_irq_nosync(priv->spi->irq);
638 priv->state = TSC2046_STATE_SHUTDOWN;
639 spin_unlock_irqrestore(&priv->state_lock, flags);
641 hrtimer_cancel(&priv->trig_timer);
647 static const struct iio_trigger_ops tsc2046_adc_trigger_ops = {
648 .set_trigger_state = tsc2046_adc_set_trigger_state,
649 .reenable = tsc2046_adc_reenable_trigger,
652 static int tsc2046_adc_setup_spi_msg(struct tsc2046_adc_priv *priv)
659 * Make dummy read to set initial power state and get real SPI clock
660 * freq. It seems to be not important which channel is used for this
663 ret = tsc2046_adc_read_one(priv, TI_TSC2046_ADDR_TEMP0,
664 &priv->effective_speed_hz);
669 * In case SPI controller do not report effective_speed_hz, use
670 * configure value and hope it will match.
672 if (!priv->effective_speed_hz)
673 priv->effective_speed_hz = priv->spi->max_speed_hz;
676 priv->scan_interval_us = TI_TSC2046_SAMPLE_INTERVAL_US;
677 priv->time_per_bit_ns = DIV_ROUND_UP(NSEC_PER_SEC,
678 priv->effective_speed_hz);
681 * Calculate and allocate maximal size buffer if all channels are
685 for (ch_idx = 0; ch_idx < ARRAY_SIZE(priv->l); ch_idx++)
686 size += tsc2046_adc_group_set_layout(priv, ch_idx, ch_idx);
688 if (size > PAGE_SIZE) {
689 dev_err(&priv->spi->dev,
690 "Calculated scan buffer is too big. Try to reduce spi-max-frequency, settling-time-us or oversampling-ratio\n");
694 priv->tx = devm_kzalloc(&priv->spi->dev, size, GFP_KERNEL);
698 priv->rx = devm_kzalloc(&priv->spi->dev, size, GFP_KERNEL);
702 priv->xfer.tx_buf = priv->tx;
703 priv->xfer.rx_buf = priv->rx;
704 priv->xfer.len = size;
705 spi_message_init_with_transfers(&priv->msg, &priv->xfer, 1);
710 static void tsc2046_adc_parse_fwnode(struct tsc2046_adc_priv *priv)
712 struct fwnode_handle *child;
713 struct device *dev = &priv->spi->dev;
716 for (i = 0; i < ARRAY_SIZE(priv->ch_cfg); i++) {
717 priv->ch_cfg[i].settling_time_us = 1;
718 priv->ch_cfg[i].oversampling_ratio = 1;
721 device_for_each_child_node(dev, child) {
725 ret = fwnode_property_read_u32(child, "reg", ®);
727 dev_err(dev, "invalid reg on %pfw, err: %pe\n", child,
732 if (reg >= ARRAY_SIZE(priv->ch_cfg)) {
733 dev_err(dev, "%pfw: Unsupported reg value: %i, max supported is: %zu.\n",
734 child, reg, ARRAY_SIZE(priv->ch_cfg));
738 ret = fwnode_property_read_u32(child, "settling-time-us", &stl);
740 priv->ch_cfg[reg].settling_time_us = stl;
742 ret = fwnode_property_read_u32(child, "oversampling-ratio",
745 priv->ch_cfg[reg].oversampling_ratio = overs;
749 static void tsc2046_adc_regulator_disable(void *data)
751 struct tsc2046_adc_priv *priv = data;
753 regulator_disable(priv->vref_reg);
756 static int tsc2046_adc_configure_regulator(struct tsc2046_adc_priv *priv)
758 struct device *dev = &priv->spi->dev;
761 priv->vref_reg = devm_regulator_get_optional(dev, "vref");
762 if (IS_ERR(priv->vref_reg)) {
763 /* If regulator exists but can't be get, return an error */
764 if (PTR_ERR(priv->vref_reg) != -ENODEV)
765 return PTR_ERR(priv->vref_reg);
766 priv->vref_reg = NULL;
768 if (!priv->vref_reg) {
769 /* Use internal reference */
770 priv->vref_mv = TI_TSC2046_INT_VREF;
774 ret = regulator_enable(priv->vref_reg);
778 ret = devm_add_action_or_reset(dev, tsc2046_adc_regulator_disable,
783 ret = regulator_get_voltage(priv->vref_reg);
787 priv->vref_mv = ret / MILLI;
792 static int tsc2046_adc_probe(struct spi_device *spi)
794 const struct tsc2046_adc_dcfg *dcfg;
795 struct device *dev = &spi->dev;
796 struct tsc2046_adc_priv *priv;
797 struct iio_dev *indio_dev;
798 struct iio_trigger *trig;
801 if (spi->max_speed_hz > TI_TSC2046_MAX_CLK_FREQ) {
802 dev_err(dev, "SPI max_speed_hz is too high: %d Hz. Max supported freq is %zu Hz\n",
803 spi->max_speed_hz, TI_TSC2046_MAX_CLK_FREQ);
807 dcfg = device_get_match_data(dev);
809 const struct spi_device_id *id = spi_get_device_id(spi);
811 dcfg = (const struct tsc2046_adc_dcfg *)id->driver_data;
816 spi->bits_per_word = 8;
817 spi->mode &= ~SPI_MODE_X_MASK;
818 spi->mode |= SPI_MODE_0;
819 ret = spi_setup(spi);
821 return dev_err_probe(dev, ret, "Error in SPI setup\n");
823 indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
827 priv = iio_priv(indio_dev);
832 indio_dev->name = TI_TSC2046_NAME;
833 indio_dev->modes = INDIO_DIRECT_MODE;
834 indio_dev->channels = dcfg->channels;
835 indio_dev->num_channels = dcfg->num_channels;
836 indio_dev->info = &tsc2046_adc_info;
838 ret = tsc2046_adc_configure_regulator(priv);
842 tsc2046_adc_parse_fwnode(priv);
844 ret = tsc2046_adc_setup_spi_msg(priv);
848 mutex_init(&priv->slock);
850 ret = devm_request_irq(dev, spi->irq, &tsc2046_adc_irq,
851 IRQF_NO_AUTOEN, indio_dev->name, indio_dev);
855 trig = devm_iio_trigger_alloc(dev, "touchscreen-%s", indio_dev->name);
860 iio_trigger_set_drvdata(trig, indio_dev);
861 trig->ops = &tsc2046_adc_trigger_ops;
863 spin_lock_init(&priv->state_lock);
864 priv->state = TSC2046_STATE_SHUTDOWN;
865 hrtimer_init(&priv->trig_timer, CLOCK_MONOTONIC,
866 HRTIMER_MODE_REL_SOFT);
867 priv->trig_timer.function = tsc2046_adc_timer;
869 ret = devm_iio_trigger_register(dev, trig);
871 dev_err(dev, "failed to register trigger\n");
875 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
876 &tsc2046_adc_trigger_handler, NULL);
878 dev_err(dev, "Failed to setup triggered buffer\n");
882 /* set default trigger */
883 indio_dev->trig = iio_trigger_get(priv->trig);
885 return devm_iio_device_register(dev, indio_dev);
888 static const struct of_device_id ads7950_of_table[] = {
889 { .compatible = "ti,tsc2046e-adc", .data = &tsc2046_adc_dcfg_tsc2046e },
892 MODULE_DEVICE_TABLE(of, ads7950_of_table);
894 static const struct spi_device_id tsc2046_adc_spi_ids[] = {
895 { "tsc2046e-adc", (unsigned long)&tsc2046_adc_dcfg_tsc2046e },
898 MODULE_DEVICE_TABLE(spi, tsc2046_adc_spi_ids);
900 static struct spi_driver tsc2046_adc_driver = {
903 .of_match_table = ads7950_of_table,
905 .id_table = tsc2046_adc_spi_ids,
906 .probe = tsc2046_adc_probe,
908 module_spi_driver(tsc2046_adc_driver);
911 MODULE_DESCRIPTION("TI TSC2046 ADC");
912 MODULE_LICENSE("GPL v2");