1 // SPDX-License-Identifier: GPL-2.0
3 * Texas Instruments TSC2046 SPI ADC driver
5 * Copyright (c) 2021 Oleksij Rempel <kernel@pengutronix.de>, Pengutronix
8 #include <linux/bitfield.h>
9 #include <linux/cleanup.h>
10 #include <linux/delay.h>
11 #include <linux/module.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/spi/spi.h>
14 #include <linux/units.h>
16 #include <linux/unaligned.h>
18 #include <linux/iio/buffer.h>
19 #include <linux/iio/trigger_consumer.h>
20 #include <linux/iio/triggered_buffer.h>
21 #include <linux/iio/trigger.h>
24 * The PENIRQ of TSC2046 controller is implemented as level shifter attached to
25 * the X+ line. If voltage of the X+ line reaches a specific level the IRQ will
26 * be activated or deactivated.
27 * To make this kind of IRQ reusable as trigger following additions were
30 * For typical touchscreen use case, we need to trigger about each 10ms.
32 * Continue triggering at least once after the IRQ was deactivated. Then
33 * deactivate this trigger to stop sampling in order to reduce power
37 #define TI_TSC2046_NAME "tsc2046"
39 /* This driver doesn't aim at the peak continuous sample rate */
40 #define TI_TSC2046_MAX_SAMPLE_RATE 125000
41 #define TI_TSC2046_SAMPLE_BITS \
42 BITS_PER_TYPE(struct tsc2046_adc_atom)
43 #define TI_TSC2046_MAX_CLK_FREQ \
44 (TI_TSC2046_MAX_SAMPLE_RATE * TI_TSC2046_SAMPLE_BITS)
46 #define TI_TSC2046_SAMPLE_INTERVAL_US 10000
48 #define TI_TSC2046_START BIT(7)
49 #define TI_TSC2046_ADDR GENMASK(6, 4)
50 #define TI_TSC2046_ADDR_TEMP1 7
51 #define TI_TSC2046_ADDR_AUX 6
52 #define TI_TSC2046_ADDR_X 5
53 #define TI_TSC2046_ADDR_Z2 4
54 #define TI_TSC2046_ADDR_Z1 3
55 #define TI_TSC2046_ADDR_VBAT 2
56 #define TI_TSC2046_ADDR_Y 1
57 #define TI_TSC2046_ADDR_TEMP0 0
60 * The mode bit sets the resolution of the ADC. With this bit low, the next
61 * conversion has 12-bit resolution, whereas with this bit high, the next
62 * conversion has 8-bit resolution. This driver is optimized for 12-bit mode.
63 * So, for this driver, this bit should stay zero.
65 #define TI_TSC2046_8BIT_MODE BIT(3)
68 * SER/DFR - The SER/DFR bit controls the reference mode, either single-ended
69 * (high) or differential (low).
71 #define TI_TSC2046_SER BIT(2)
74 * If VREF_ON and ADC_ON are both zero, then the chip operates in
75 * auto-wake/suspend mode. In most case this bits should stay zero.
77 #define TI_TSC2046_PD1_VREF_ON BIT(1)
78 #define TI_TSC2046_PD0_ADC_ON BIT(0)
81 * All supported devices can do 8 or 12bit resolution. This driver
82 * supports only 12bit mode, here we have a 16bit data transfer, where
83 * the MSB and the 3 LSB are 0.
85 #define TI_TSC2046_DATA_12BIT GENMASK(14, 3)
87 #define TI_TSC2046_MAX_CHAN 8
88 #define TI_TSC2046_MIN_POLL_CNT 3
89 #define TI_TSC2046_EXT_POLL_CNT 3
90 #define TI_TSC2046_POLL_CNT \
91 (TI_TSC2046_MIN_POLL_CNT + TI_TSC2046_EXT_POLL_CNT)
92 #define TI_TSC2046_INT_VREF 2500
94 /* Represents a HW sample */
95 struct tsc2046_adc_atom
{
97 * Command transmitted to the controller. This field is empty on the RX
102 * Data received from the controller. This field is empty for the TX
108 /* Layout of atomic buffers within big buffer */
109 struct tsc2046_adc_group_layout
{
110 /* Group offset within the SPI RX buffer */
113 * Amount of tsc2046_adc_atom structs within the same command gathered
118 * Settling samples (tsc2046_adc_atom structs) which should be skipped
119 * before good samples will start.
124 struct tsc2046_adc_dcfg
{
125 const struct iio_chan_spec
*channels
;
126 unsigned int num_channels
;
129 struct tsc2046_adc_ch_cfg
{
130 unsigned int settling_time_us
;
131 unsigned int oversampling_ratio
;
135 TSC2046_STATE_SHUTDOWN
,
136 TSC2046_STATE_STANDBY
,
138 TSC2046_STATE_POLL_IRQ_DISABLE
,
139 TSC2046_STATE_ENABLE_IRQ
,
142 struct tsc2046_adc_priv
{
143 struct spi_device
*spi
;
144 const struct tsc2046_adc_dcfg
*dcfg
;
147 struct iio_trigger
*trig
;
148 struct hrtimer trig_timer
;
149 enum tsc2046_state state
;
151 spinlock_t state_lock
;
153 struct spi_transfer xfer
;
154 struct spi_message msg
;
157 /* Scan data for each channel */
158 u16 data
[TI_TSC2046_MAX_CHAN
];
164 * Lock to protect the layout and the SPI transfer buffer.
165 * tsc2046_adc_group_layout can be changed within update_scan_mode(),
166 * in this case the l[] and tx/rx buffer will be out of sync to each
170 struct tsc2046_adc_group_layout l
[TI_TSC2046_MAX_CHAN
];
171 struct tsc2046_adc_atom
*rx
;
172 struct tsc2046_adc_atom
*tx
;
176 u32 effective_speed_hz
;
177 u32 scan_interval_us
;
178 u32 time_per_scan_us
;
180 unsigned int vref_mv
;
182 struct tsc2046_adc_ch_cfg ch_cfg
[TI_TSC2046_MAX_CHAN
];
185 #define TI_TSC2046_V_CHAN(index, bits, name) \
187 .type = IIO_VOLTAGE, \
190 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
191 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
192 .datasheet_name = "#name", \
193 .scan_index = index, \
198 .endianness = IIO_CPU, \
202 #define DECLARE_TI_TSC2046_8_CHANNELS(name, bits) \
203 const struct iio_chan_spec name ## _channels[] = { \
204 TI_TSC2046_V_CHAN(0, bits, TEMP0), \
205 TI_TSC2046_V_CHAN(1, bits, Y), \
206 TI_TSC2046_V_CHAN(2, bits, VBAT), \
207 TI_TSC2046_V_CHAN(3, bits, Z1), \
208 TI_TSC2046_V_CHAN(4, bits, Z2), \
209 TI_TSC2046_V_CHAN(5, bits, X), \
210 TI_TSC2046_V_CHAN(6, bits, AUX), \
211 TI_TSC2046_V_CHAN(7, bits, TEMP1), \
212 IIO_CHAN_SOFT_TIMESTAMP(8), \
215 static DECLARE_TI_TSC2046_8_CHANNELS(tsc2046_adc
, 12);
217 static const struct tsc2046_adc_dcfg tsc2046_adc_dcfg_tsc2046e
= {
218 .channels
= tsc2046_adc_channels
,
219 .num_channels
= ARRAY_SIZE(tsc2046_adc_channels
),
223 * Convert time to a number of samples which can be transferred within this
226 static unsigned int tsc2046_adc_time_to_count(struct tsc2046_adc_priv
*priv
,
229 unsigned int bit_count
, sample_count
;
231 bit_count
= DIV_ROUND_UP(time
* NSEC_PER_USEC
, priv
->time_per_bit_ns
);
232 sample_count
= DIV_ROUND_UP(bit_count
, TI_TSC2046_SAMPLE_BITS
);
234 dev_dbg(&priv
->spi
->dev
, "Effective speed %u, time per bit: %u, count bits: %u, count samples: %u\n",
235 priv
->effective_speed_hz
, priv
->time_per_bit_ns
,
236 bit_count
, sample_count
);
241 static u8
tsc2046_adc_get_cmd(struct tsc2046_adc_priv
*priv
, int ch_idx
,
247 * if PD bits are 0, controller will automatically disable ADC, VREF and
251 pd
= TI_TSC2046_PD0_ADC_ON
;
256 case TI_TSC2046_ADDR_TEMP1
:
257 case TI_TSC2046_ADDR_AUX
:
258 case TI_TSC2046_ADDR_VBAT
:
259 case TI_TSC2046_ADDR_TEMP0
:
260 pd
|= TI_TSC2046_SER
;
261 if (priv
->internal_vref
)
262 pd
|= TI_TSC2046_PD1_VREF_ON
;
265 return TI_TSC2046_START
| FIELD_PREP(TI_TSC2046_ADDR
, ch_idx
) | pd
;
268 static u16
tsc2046_adc_get_value(struct tsc2046_adc_atom
*buf
)
270 return FIELD_GET(TI_TSC2046_DATA_12BIT
, get_unaligned_be16(&buf
->data
));
273 static int tsc2046_adc_read_one(struct tsc2046_adc_priv
*priv
, int ch_idx
,
274 u32
*effective_speed_hz
)
276 struct tsc2046_adc_ch_cfg
*ch
= &priv
->ch_cfg
[ch_idx
];
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(struct tsc2046_adc_atom
) * max_count
> PAGE_SIZE
)
293 struct tsc2046_adc_atom
*tx_buf
__free(kfree
) = kcalloc(max_count
,
299 struct tsc2046_adc_atom
*rx_buf
__free(kfree
) = kcalloc(max_count
,
306 * Do not enable automatic power down on working samples. Otherwise the
307 * plates will never be completely charged.
309 cmd
= tsc2046_adc_get_cmd(priv
, ch_idx
, true);
311 for (i
= 0; i
< max_count
- 1; i
++)
314 /* automatically power down on last sample */
315 tx_buf
[i
].cmd
= tsc2046_adc_get_cmd(priv
, ch_idx
, false);
317 memset(&xfer
, 0, sizeof(xfer
));
318 xfer
.tx_buf
= tx_buf
;
319 xfer
.rx_buf
= rx_buf
;
320 xfer
.len
= sizeof(*tx_buf
) * max_count
;
321 spi_message_init_with_transfers(&msg
, &xfer
, 1);
324 * We aren't using spi_write_then_read() because we need to be able
325 * to get hold of the effective_speed_hz from the xfer
327 ret
= spi_sync(priv
->spi
, &msg
);
329 dev_err_ratelimited(&priv
->spi
->dev
, "SPI transfer failed %pe\n",
334 if (effective_speed_hz
)
335 *effective_speed_hz
= xfer
.effective_speed_hz
;
337 for (i
= 0; i
< max_count
- count_skip
; i
++) {
338 val
= tsc2046_adc_get_value(&rx_buf
[count_skip
+ i
]);
339 val_normalized
+= val
;
342 return DIV_ROUND_UP(val_normalized
, max_count
- count_skip
);
345 static size_t tsc2046_adc_group_set_layout(struct tsc2046_adc_priv
*priv
,
349 struct tsc2046_adc_ch_cfg
*ch
= &priv
->ch_cfg
[ch_idx
];
350 struct tsc2046_adc_group_layout
*cur
;
351 unsigned int max_count
, count_skip
;
352 unsigned int offset
= 0;
355 offset
= priv
->l
[group
- 1].offset
+ priv
->l
[group
- 1].count
;
357 count_skip
= tsc2046_adc_time_to_count(priv
, ch
->settling_time_us
);
358 max_count
= count_skip
+ ch
->oversampling_ratio
;
360 cur
= &priv
->l
[group
];
361 cur
->offset
= offset
;
362 cur
->count
= max_count
;
363 cur
->skip
= count_skip
;
365 return sizeof(*priv
->tx
) * max_count
;
368 static void tsc2046_adc_group_set_cmd(struct tsc2046_adc_priv
*priv
,
369 unsigned int group
, int ch_idx
)
371 struct tsc2046_adc_group_layout
*l
= &priv
->l
[group
];
376 * Do not enable automatic power down on working samples. Otherwise the
377 * plates will never be completely charged.
379 cmd
= tsc2046_adc_get_cmd(priv
, ch_idx
, true);
381 for (i
= 0; i
< l
->count
- 1; i
++)
382 priv
->tx
[l
->offset
+ i
].cmd
= cmd
;
384 /* automatically power down on last sample */
385 priv
->tx
[l
->offset
+ i
].cmd
= tsc2046_adc_get_cmd(priv
, ch_idx
, false);
388 static u16
tsc2046_adc_get_val(struct tsc2046_adc_priv
*priv
, int group
)
390 struct tsc2046_adc_group_layout
*l
;
391 unsigned int val
, val_normalized
= 0;
395 valid_count
= l
->count
- l
->skip
;
397 for (i
= 0; i
< valid_count
; i
++) {
398 val
= tsc2046_adc_get_value(&priv
->rx
[l
->offset
+ l
->skip
+ i
]);
399 val_normalized
+= val
;
402 return DIV_ROUND_UP(val_normalized
, valid_count
);
405 static int tsc2046_adc_scan(struct iio_dev
*indio_dev
)
407 struct tsc2046_adc_priv
*priv
= iio_priv(indio_dev
);
408 struct device
*dev
= &priv
->spi
->dev
;
412 ret
= spi_sync(priv
->spi
, &priv
->msg
);
414 dev_err_ratelimited(dev
, "SPI transfer failed: %pe\n", ERR_PTR(ret
));
418 for (group
= 0; group
< priv
->groups
; group
++)
419 priv
->scan_buf
.data
[group
] = tsc2046_adc_get_val(priv
, group
);
421 ret
= iio_push_to_buffers_with_timestamp(indio_dev
, &priv
->scan_buf
,
422 iio_get_time_ns(indio_dev
));
423 /* If the consumer is kfifo, we may get a EBUSY here - ignore it. */
424 if (ret
< 0 && ret
!= -EBUSY
) {
425 dev_err_ratelimited(dev
, "Failed to push scan buffer %pe\n",
434 static irqreturn_t
tsc2046_adc_trigger_handler(int irq
, void *p
)
436 struct iio_poll_func
*pf
= p
;
437 struct iio_dev
*indio_dev
= pf
->indio_dev
;
438 struct tsc2046_adc_priv
*priv
= iio_priv(indio_dev
);
440 mutex_lock(&priv
->slock
);
441 tsc2046_adc_scan(indio_dev
);
442 mutex_unlock(&priv
->slock
);
444 iio_trigger_notify_done(indio_dev
->trig
);
449 static int tsc2046_adc_read_raw(struct iio_dev
*indio_dev
,
450 struct iio_chan_spec
const *chan
,
451 int *val
, int *val2
, long m
)
453 struct tsc2046_adc_priv
*priv
= iio_priv(indio_dev
);
457 case IIO_CHAN_INFO_RAW
:
458 ret
= tsc2046_adc_read_one(priv
, chan
->channel
, NULL
);
465 case IIO_CHAN_INFO_SCALE
:
467 * Note: the TSC2046 has internal voltage divider on the VBAT
468 * line. This divider can be influenced by external divider.
469 * So, it is better to use external voltage-divider driver
470 * instead, which is calculating complete chain.
472 *val
= priv
->vref_mv
;
473 *val2
= chan
->scan_type
.realbits
;
474 return IIO_VAL_FRACTIONAL_LOG2
;
480 static int tsc2046_adc_update_scan_mode(struct iio_dev
*indio_dev
,
481 const unsigned long *active_scan_mask
)
483 struct tsc2046_adc_priv
*priv
= iio_priv(indio_dev
);
484 unsigned int ch_idx
, group
= 0;
487 mutex_lock(&priv
->slock
);
490 for_each_set_bit(ch_idx
, active_scan_mask
, ARRAY_SIZE(priv
->l
)) {
491 size
+= tsc2046_adc_group_set_layout(priv
, group
, ch_idx
);
492 tsc2046_adc_group_set_cmd(priv
, group
, ch_idx
);
496 priv
->groups
= group
;
497 priv
->xfer
.len
= size
;
498 priv
->time_per_scan_us
= size
* 8 * priv
->time_per_bit_ns
/ NSEC_PER_USEC
;
500 if (priv
->scan_interval_us
< priv
->time_per_scan_us
)
501 dev_warn(&priv
->spi
->dev
, "The scan interval (%d) is less then calculated scan time (%d)\n",
502 priv
->scan_interval_us
, priv
->time_per_scan_us
);
504 mutex_unlock(&priv
->slock
);
509 static const struct iio_info tsc2046_adc_info
= {
510 .read_raw
= tsc2046_adc_read_raw
,
511 .update_scan_mode
= tsc2046_adc_update_scan_mode
,
514 static enum hrtimer_restart
tsc2046_adc_timer(struct hrtimer
*hrtimer
)
516 struct tsc2046_adc_priv
*priv
= container_of(hrtimer
,
517 struct tsc2046_adc_priv
,
522 * This state machine should address following challenges :
523 * - the interrupt source is based on level shifter attached to the X
524 * channel of ADC. It will change the state every time we switch
525 * between channels. So, we need to disable IRQ if we do
526 * iio_trigger_poll().
527 * - we should do iio_trigger_poll() at some reduced sample rate
528 * - we should still trigger for some amount of time after last
529 * interrupt with enabled IRQ was processed.
532 spin_lock_irqsave(&priv
->state_lock
, flags
);
533 switch (priv
->state
) {
534 case TSC2046_STATE_ENABLE_IRQ
:
535 if (priv
->poll_cnt
< TI_TSC2046_POLL_CNT
) {
537 hrtimer_start(&priv
->trig_timer
,
538 ns_to_ktime(priv
->scan_interval_us
*
540 HRTIMER_MODE_REL_SOFT
);
542 if (priv
->poll_cnt
>= TI_TSC2046_MIN_POLL_CNT
) {
543 priv
->state
= TSC2046_STATE_POLL_IRQ_DISABLE
;
544 enable_irq(priv
->spi
->irq
);
546 priv
->state
= TSC2046_STATE_POLL
;
549 priv
->state
= TSC2046_STATE_STANDBY
;
550 enable_irq(priv
->spi
->irq
);
553 case TSC2046_STATE_POLL_IRQ_DISABLE
:
554 disable_irq_nosync(priv
->spi
->irq
);
556 case TSC2046_STATE_POLL
:
557 priv
->state
= TSC2046_STATE_ENABLE_IRQ
;
558 /* iio_trigger_poll() starts hrtimer */
559 iio_trigger_poll(priv
->trig
);
561 case TSC2046_STATE_SHUTDOWN
:
563 case TSC2046_STATE_STANDBY
:
566 dev_warn(&priv
->spi
->dev
, "Got unexpected state: %i\n",
570 spin_unlock_irqrestore(&priv
->state_lock
, flags
);
572 return HRTIMER_NORESTART
;
575 static irqreturn_t
tsc2046_adc_irq(int irq
, void *dev_id
)
577 struct iio_dev
*indio_dev
= dev_id
;
578 struct tsc2046_adc_priv
*priv
= iio_priv(indio_dev
);
581 hrtimer_try_to_cancel(&priv
->trig_timer
);
583 spin_lock_irqsave(&priv
->state_lock
, flags
);
584 if (priv
->state
!= TSC2046_STATE_SHUTDOWN
) {
585 priv
->state
= TSC2046_STATE_ENABLE_IRQ
;
588 /* iio_trigger_poll() starts hrtimer */
589 disable_irq_nosync(priv
->spi
->irq
);
590 iio_trigger_poll(priv
->trig
);
592 spin_unlock_irqrestore(&priv
->state_lock
, flags
);
597 static void tsc2046_adc_reenable_trigger(struct iio_trigger
*trig
)
599 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
600 struct tsc2046_adc_priv
*priv
= iio_priv(indio_dev
);
604 * We can sample it as fast as we can, but usually we do not need so
605 * many samples. Reduce the sample rate for default (touchscreen) use
608 tim
= ns_to_ktime((priv
->scan_interval_us
- priv
->time_per_scan_us
) *
610 hrtimer_start(&priv
->trig_timer
, tim
, HRTIMER_MODE_REL_SOFT
);
613 static int tsc2046_adc_set_trigger_state(struct iio_trigger
*trig
, bool enable
)
615 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
616 struct tsc2046_adc_priv
*priv
= iio_priv(indio_dev
);
620 spin_lock_irqsave(&priv
->state_lock
, flags
);
621 if (priv
->state
== TSC2046_STATE_SHUTDOWN
) {
622 priv
->state
= TSC2046_STATE_STANDBY
;
623 enable_irq(priv
->spi
->irq
);
625 spin_unlock_irqrestore(&priv
->state_lock
, flags
);
627 spin_lock_irqsave(&priv
->state_lock
, flags
);
629 if (priv
->state
== TSC2046_STATE_STANDBY
||
630 priv
->state
== TSC2046_STATE_POLL_IRQ_DISABLE
)
631 disable_irq_nosync(priv
->spi
->irq
);
633 priv
->state
= TSC2046_STATE_SHUTDOWN
;
634 spin_unlock_irqrestore(&priv
->state_lock
, flags
);
636 hrtimer_cancel(&priv
->trig_timer
);
642 static const struct iio_trigger_ops tsc2046_adc_trigger_ops
= {
643 .set_trigger_state
= tsc2046_adc_set_trigger_state
,
644 .reenable
= tsc2046_adc_reenable_trigger
,
647 static int tsc2046_adc_setup_spi_msg(struct tsc2046_adc_priv
*priv
)
654 * Make dummy read to set initial power state and get real SPI clock
655 * freq. It seems to be not important which channel is used for this
658 ret
= tsc2046_adc_read_one(priv
, TI_TSC2046_ADDR_TEMP0
,
659 &priv
->effective_speed_hz
);
664 * In case SPI controller do not report effective_speed_hz, use
665 * configure value and hope it will match.
667 if (!priv
->effective_speed_hz
)
668 priv
->effective_speed_hz
= priv
->spi
->max_speed_hz
;
671 priv
->scan_interval_us
= TI_TSC2046_SAMPLE_INTERVAL_US
;
672 priv
->time_per_bit_ns
= DIV_ROUND_UP(NSEC_PER_SEC
,
673 priv
->effective_speed_hz
);
676 * Calculate and allocate maximal size buffer if all channels are
680 for (ch_idx
= 0; ch_idx
< ARRAY_SIZE(priv
->l
); ch_idx
++)
681 size
+= tsc2046_adc_group_set_layout(priv
, ch_idx
, ch_idx
);
683 if (size
> PAGE_SIZE
) {
684 dev_err(&priv
->spi
->dev
,
685 "Calculated scan buffer is too big. Try to reduce spi-max-frequency, settling-time-us or oversampling-ratio\n");
689 priv
->tx
= devm_kzalloc(&priv
->spi
->dev
, size
, GFP_KERNEL
);
693 priv
->rx
= devm_kzalloc(&priv
->spi
->dev
, size
, GFP_KERNEL
);
697 priv
->xfer
.tx_buf
= priv
->tx
;
698 priv
->xfer
.rx_buf
= priv
->rx
;
699 priv
->xfer
.len
= size
;
700 spi_message_init_with_transfers(&priv
->msg
, &priv
->xfer
, 1);
705 static void tsc2046_adc_parse_fwnode(struct tsc2046_adc_priv
*priv
)
707 struct fwnode_handle
*child
;
708 struct device
*dev
= &priv
->spi
->dev
;
711 for (i
= 0; i
< ARRAY_SIZE(priv
->ch_cfg
); i
++) {
712 priv
->ch_cfg
[i
].settling_time_us
= 1;
713 priv
->ch_cfg
[i
].oversampling_ratio
= 1;
716 device_for_each_child_node(dev
, child
) {
720 ret
= fwnode_property_read_u32(child
, "reg", ®
);
722 dev_err(dev
, "invalid reg on %pfw, err: %pe\n", child
,
727 if (reg
>= ARRAY_SIZE(priv
->ch_cfg
)) {
728 dev_err(dev
, "%pfw: Unsupported reg value: %i, max supported is: %zu.\n",
729 child
, reg
, ARRAY_SIZE(priv
->ch_cfg
));
733 ret
= fwnode_property_read_u32(child
, "settling-time-us", &stl
);
735 priv
->ch_cfg
[reg
].settling_time_us
= stl
;
737 ret
= fwnode_property_read_u32(child
, "oversampling-ratio",
740 priv
->ch_cfg
[reg
].oversampling_ratio
= overs
;
744 static int tsc2046_adc_probe(struct spi_device
*spi
)
746 const struct tsc2046_adc_dcfg
*dcfg
;
747 struct device
*dev
= &spi
->dev
;
748 struct tsc2046_adc_priv
*priv
;
749 struct iio_dev
*indio_dev
;
750 struct iio_trigger
*trig
;
753 if (spi
->max_speed_hz
> TI_TSC2046_MAX_CLK_FREQ
) {
754 dev_err(dev
, "SPI max_speed_hz is too high: %d Hz. Max supported freq is %zu Hz\n",
755 spi
->max_speed_hz
, TI_TSC2046_MAX_CLK_FREQ
);
759 dcfg
= spi_get_device_match_data(spi
);
763 spi
->bits_per_word
= 8;
764 spi
->mode
&= ~SPI_MODE_X_MASK
;
765 spi
->mode
|= SPI_MODE_0
;
766 ret
= spi_setup(spi
);
768 return dev_err_probe(dev
, ret
, "Error in SPI setup\n");
770 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*priv
));
774 priv
= iio_priv(indio_dev
);
779 indio_dev
->name
= TI_TSC2046_NAME
;
780 indio_dev
->modes
= INDIO_DIRECT_MODE
;
781 indio_dev
->channels
= dcfg
->channels
;
782 indio_dev
->num_channels
= dcfg
->num_channels
;
783 indio_dev
->info
= &tsc2046_adc_info
;
785 ret
= devm_regulator_get_enable_read_voltage(dev
, "vref");
786 if (ret
< 0 && ret
!= -ENODEV
)
789 priv
->internal_vref
= ret
== -ENODEV
;
790 priv
->vref_mv
= priv
->internal_vref
? TI_TSC2046_INT_VREF
: ret
/ MILLI
;
792 tsc2046_adc_parse_fwnode(priv
);
794 ret
= tsc2046_adc_setup_spi_msg(priv
);
798 mutex_init(&priv
->slock
);
800 ret
= devm_request_irq(dev
, spi
->irq
, &tsc2046_adc_irq
,
801 IRQF_NO_AUTOEN
, indio_dev
->name
, indio_dev
);
805 trig
= devm_iio_trigger_alloc(dev
, "touchscreen-%s", indio_dev
->name
);
810 iio_trigger_set_drvdata(trig
, indio_dev
);
811 trig
->ops
= &tsc2046_adc_trigger_ops
;
813 spin_lock_init(&priv
->state_lock
);
814 priv
->state
= TSC2046_STATE_SHUTDOWN
;
815 hrtimer_init(&priv
->trig_timer
, CLOCK_MONOTONIC
,
816 HRTIMER_MODE_REL_SOFT
);
817 priv
->trig_timer
.function
= tsc2046_adc_timer
;
819 ret
= devm_iio_trigger_register(dev
, trig
);
821 dev_err(dev
, "failed to register trigger\n");
825 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
, NULL
,
826 &tsc2046_adc_trigger_handler
, NULL
);
828 dev_err(dev
, "Failed to setup triggered buffer\n");
832 /* set default trigger */
833 indio_dev
->trig
= iio_trigger_get(priv
->trig
);
835 return devm_iio_device_register(dev
, indio_dev
);
838 static const struct of_device_id ads7950_of_table
[] = {
839 { .compatible
= "ti,tsc2046e-adc", .data
= &tsc2046_adc_dcfg_tsc2046e
},
842 MODULE_DEVICE_TABLE(of
, ads7950_of_table
);
844 static const struct spi_device_id tsc2046_adc_spi_ids
[] = {
845 { "tsc2046e-adc", (unsigned long)&tsc2046_adc_dcfg_tsc2046e
},
848 MODULE_DEVICE_TABLE(spi
, tsc2046_adc_spi_ids
);
850 static struct spi_driver tsc2046_adc_driver
= {
853 .of_match_table
= ads7950_of_table
,
855 .id_table
= tsc2046_adc_spi_ids
,
856 .probe
= tsc2046_adc_probe
,
858 module_spi_driver(tsc2046_adc_driver
);
860 MODULE_AUTHOR("Oleksij Rempel <kernel@pengutronix.de>");
861 MODULE_DESCRIPTION("TI TSC2046 ADC");
862 MODULE_LICENSE("GPL v2");