ti-tsc2046.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Texas Instruments TSC2046 SPI ADC driver
  4. *
  5. * Copyright (c) 2021 Oleksij Rempel <[email protected]>, Pengutronix
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/delay.h>
  9. #include <linux/module.h>
  10. #include <linux/regulator/consumer.h>
  11. #include <linux/spi/spi.h>
  12. #include <linux/units.h>
  13. #include <asm/unaligned.h>
  14. #include <linux/iio/buffer.h>
  15. #include <linux/iio/trigger_consumer.h>
  16. #include <linux/iio/triggered_buffer.h>
  17. #include <linux/iio/trigger.h>
  18. /*
  19. * The PENIRQ of TSC2046 controller is implemented as level shifter attached to
  20. * the X+ line. If voltage of the X+ line reaches a specific level the IRQ will
  21. * be activated or deactivated.
  22. * To make this kind of IRQ reusable as trigger following additions were
  23. * implemented:
  24. * - rate limiting:
  25. * For typical touchscreen use case, we need to trigger about each 10ms.
  26. * - hrtimer:
  27. * Continue triggering at least once after the IRQ was deactivated. Then
  28. * deactivate this trigger to stop sampling in order to reduce power
  29. * consumption.
  30. */
  31. #define TI_TSC2046_NAME "tsc2046"
  32. /* This driver doesn't aim at the peak continuous sample rate */
  33. #define TI_TSC2046_MAX_SAMPLE_RATE 125000
  34. #define TI_TSC2046_SAMPLE_BITS \
  35. BITS_PER_TYPE(struct tsc2046_adc_atom)
  36. #define TI_TSC2046_MAX_CLK_FREQ \
  37. (TI_TSC2046_MAX_SAMPLE_RATE * TI_TSC2046_SAMPLE_BITS)
  38. #define TI_TSC2046_SAMPLE_INTERVAL_US 10000
  39. #define TI_TSC2046_START BIT(7)
  40. #define TI_TSC2046_ADDR GENMASK(6, 4)
  41. #define TI_TSC2046_ADDR_TEMP1 7
  42. #define TI_TSC2046_ADDR_AUX 6
  43. #define TI_TSC2046_ADDR_X 5
  44. #define TI_TSC2046_ADDR_Z2 4
  45. #define TI_TSC2046_ADDR_Z1 3
  46. #define TI_TSC2046_ADDR_VBAT 2
  47. #define TI_TSC2046_ADDR_Y 1
  48. #define TI_TSC2046_ADDR_TEMP0 0
  49. /*
  50. * The mode bit sets the resolution of the ADC. With this bit low, the next
  51. * conversion has 12-bit resolution, whereas with this bit high, the next
  52. * conversion has 8-bit resolution. This driver is optimized for 12-bit mode.
  53. * So, for this driver, this bit should stay zero.
  54. */
  55. #define TI_TSC2046_8BIT_MODE BIT(3)
  56. /*
  57. * SER/DFR - The SER/DFR bit controls the reference mode, either single-ended
  58. * (high) or differential (low).
  59. */
  60. #define TI_TSC2046_SER BIT(2)
  61. /*
  62. * If VREF_ON and ADC_ON are both zero, then the chip operates in
  63. * auto-wake/suspend mode. In most case this bits should stay zero.
  64. */
  65. #define TI_TSC2046_PD1_VREF_ON BIT(1)
  66. #define TI_TSC2046_PD0_ADC_ON BIT(0)
  67. /*
  68. * All supported devices can do 8 or 12bit resolution. This driver
  69. * supports only 12bit mode, here we have a 16bit data transfer, where
  70. * the MSB and the 3 LSB are 0.
  71. */
  72. #define TI_TSC2046_DATA_12BIT GENMASK(14, 3)
  73. #define TI_TSC2046_MAX_CHAN 8
  74. #define TI_TSC2046_MIN_POLL_CNT 3
  75. #define TI_TSC2046_EXT_POLL_CNT 3
  76. #define TI_TSC2046_POLL_CNT \
  77. (TI_TSC2046_MIN_POLL_CNT + TI_TSC2046_EXT_POLL_CNT)
  78. #define TI_TSC2046_INT_VREF 2500
  79. /* Represents a HW sample */
  80. struct tsc2046_adc_atom {
  81. /*
  82. * Command transmitted to the controller. This field is empty on the RX
  83. * buffer.
  84. */
  85. u8 cmd;
  86. /*
  87. * Data received from the controller. This field is empty for the TX
  88. * buffer
  89. */
  90. __be16 data;
  91. } __packed;
  92. /* Layout of atomic buffers within big buffer */
  93. struct tsc2046_adc_group_layout {
  94. /* Group offset within the SPI RX buffer */
  95. unsigned int offset;
  96. /*
  97. * Amount of tsc2046_adc_atom structs within the same command gathered
  98. * within same group.
  99. */
  100. unsigned int count;
  101. /*
  102. * Settling samples (tsc2046_adc_atom structs) which should be skipped
  103. * before good samples will start.
  104. */
  105. unsigned int skip;
  106. };
  107. struct tsc2046_adc_dcfg {
  108. const struct iio_chan_spec *channels;
  109. unsigned int num_channels;
  110. };
  111. struct tsc2046_adc_ch_cfg {
  112. unsigned int settling_time_us;
  113. unsigned int oversampling_ratio;
  114. };
  115. enum tsc2046_state {
  116. TSC2046_STATE_SHUTDOWN,
  117. TSC2046_STATE_STANDBY,
  118. TSC2046_STATE_POLL,
  119. TSC2046_STATE_POLL_IRQ_DISABLE,
  120. TSC2046_STATE_ENABLE_IRQ,
  121. };
  122. struct tsc2046_adc_priv {
  123. struct spi_device *spi;
  124. const struct tsc2046_adc_dcfg *dcfg;
  125. struct regulator *vref_reg;
  126. struct iio_trigger *trig;
  127. struct hrtimer trig_timer;
  128. enum tsc2046_state state;
  129. int poll_cnt;
  130. spinlock_t state_lock;
  131. struct spi_transfer xfer;
  132. struct spi_message msg;
  133. struct {
  134. /* Scan data for each channel */
  135. u16 data[TI_TSC2046_MAX_CHAN];
  136. /* Timestamp */
  137. s64 ts __aligned(8);
  138. } scan_buf;
  139. /*
  140. * Lock to protect the layout and the SPI transfer buffer.
  141. * tsc2046_adc_group_layout can be changed within update_scan_mode(),
  142. * in this case the l[] and tx/rx buffer will be out of sync to each
  143. * other.
  144. */
  145. struct mutex slock;
  146. struct tsc2046_adc_group_layout l[TI_TSC2046_MAX_CHAN];
  147. struct tsc2046_adc_atom *rx;
  148. struct tsc2046_adc_atom *tx;
  149. unsigned int count;
  150. unsigned int groups;
  151. u32 effective_speed_hz;
  152. u32 scan_interval_us;
  153. u32 time_per_scan_us;
  154. u32 time_per_bit_ns;
  155. unsigned int vref_mv;
  156. struct tsc2046_adc_ch_cfg ch_cfg[TI_TSC2046_MAX_CHAN];
  157. };
  158. #define TI_TSC2046_V_CHAN(index, bits, name) \
  159. { \
  160. .type = IIO_VOLTAGE, \
  161. .indexed = 1, \
  162. .channel = index, \
  163. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  164. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  165. .datasheet_name = "#name", \
  166. .scan_index = index, \
  167. .scan_type = { \
  168. .sign = 'u', \
  169. .realbits = bits, \
  170. .storagebits = 16, \
  171. .endianness = IIO_CPU, \
  172. }, \
  173. }
  174. #define DECLARE_TI_TSC2046_8_CHANNELS(name, bits) \
  175. const struct iio_chan_spec name ## _channels[] = { \
  176. TI_TSC2046_V_CHAN(0, bits, TEMP0), \
  177. TI_TSC2046_V_CHAN(1, bits, Y), \
  178. TI_TSC2046_V_CHAN(2, bits, VBAT), \
  179. TI_TSC2046_V_CHAN(3, bits, Z1), \
  180. TI_TSC2046_V_CHAN(4, bits, Z2), \
  181. TI_TSC2046_V_CHAN(5, bits, X), \
  182. TI_TSC2046_V_CHAN(6, bits, AUX), \
  183. TI_TSC2046_V_CHAN(7, bits, TEMP1), \
  184. IIO_CHAN_SOFT_TIMESTAMP(8), \
  185. }
  186. static DECLARE_TI_TSC2046_8_CHANNELS(tsc2046_adc, 12);
  187. static const struct tsc2046_adc_dcfg tsc2046_adc_dcfg_tsc2046e = {
  188. .channels = tsc2046_adc_channels,
  189. .num_channels = ARRAY_SIZE(tsc2046_adc_channels),
  190. };
  191. /*
  192. * Convert time to a number of samples which can be transferred within this
  193. * time.
  194. */
  195. static unsigned int tsc2046_adc_time_to_count(struct tsc2046_adc_priv *priv,
  196. unsigned long time)
  197. {
  198. unsigned int bit_count, sample_count;
  199. bit_count = DIV_ROUND_UP(time * NSEC_PER_USEC, priv->time_per_bit_ns);
  200. sample_count = DIV_ROUND_UP(bit_count, TI_TSC2046_SAMPLE_BITS);
  201. dev_dbg(&priv->spi->dev, "Effective speed %u, time per bit: %u, count bits: %u, count samples: %u\n",
  202. priv->effective_speed_hz, priv->time_per_bit_ns,
  203. bit_count, sample_count);
  204. return sample_count;
  205. }
  206. static u8 tsc2046_adc_get_cmd(struct tsc2046_adc_priv *priv, int ch_idx,
  207. bool keep_power)
  208. {
  209. u32 pd;
  210. /*
  211. * if PD bits are 0, controller will automatically disable ADC, VREF and
  212. * enable IRQ.
  213. */
  214. if (keep_power)
  215. pd = TI_TSC2046_PD0_ADC_ON;
  216. else
  217. pd = 0;
  218. switch (ch_idx) {
  219. case TI_TSC2046_ADDR_TEMP1:
  220. case TI_TSC2046_ADDR_AUX:
  221. case TI_TSC2046_ADDR_VBAT:
  222. case TI_TSC2046_ADDR_TEMP0:
  223. pd |= TI_TSC2046_SER;
  224. if (!priv->vref_reg)
  225. pd |= TI_TSC2046_PD1_VREF_ON;
  226. }
  227. return TI_TSC2046_START | FIELD_PREP(TI_TSC2046_ADDR, ch_idx) | pd;
  228. }
  229. static u16 tsc2046_adc_get_value(struct tsc2046_adc_atom *buf)
  230. {
  231. return FIELD_GET(TI_TSC2046_DATA_12BIT, get_unaligned_be16(&buf->data));
  232. }
  233. static int tsc2046_adc_read_one(struct tsc2046_adc_priv *priv, int ch_idx,
  234. u32 *effective_speed_hz)
  235. {
  236. struct tsc2046_adc_ch_cfg *ch = &priv->ch_cfg[ch_idx];
  237. struct tsc2046_adc_atom *rx_buf, *tx_buf;
  238. unsigned int val, val_normalized = 0;
  239. int ret, i, count_skip = 0, max_count;
  240. struct spi_transfer xfer;
  241. struct spi_message msg;
  242. u8 cmd;
  243. if (!effective_speed_hz) {
  244. count_skip = tsc2046_adc_time_to_count(priv, ch->settling_time_us);
  245. max_count = count_skip + ch->oversampling_ratio;
  246. } else {
  247. max_count = 1;
  248. }
  249. if (sizeof(*tx_buf) * max_count > PAGE_SIZE)
  250. return -ENOSPC;
  251. tx_buf = kcalloc(max_count, sizeof(*tx_buf), GFP_KERNEL);
  252. if (!tx_buf)
  253. return -ENOMEM;
  254. rx_buf = kcalloc(max_count, sizeof(*rx_buf), GFP_KERNEL);
  255. if (!rx_buf) {
  256. ret = -ENOMEM;
  257. goto free_tx;
  258. }
  259. /*
  260. * Do not enable automatic power down on working samples. Otherwise the
  261. * plates will never be completely charged.
  262. */
  263. cmd = tsc2046_adc_get_cmd(priv, ch_idx, true);
  264. for (i = 0; i < max_count - 1; i++)
  265. tx_buf[i].cmd = cmd;
  266. /* automatically power down on last sample */
  267. tx_buf[i].cmd = tsc2046_adc_get_cmd(priv, ch_idx, false);
  268. memset(&xfer, 0, sizeof(xfer));
  269. xfer.tx_buf = tx_buf;
  270. xfer.rx_buf = rx_buf;
  271. xfer.len = sizeof(*tx_buf) * max_count;
  272. spi_message_init_with_transfers(&msg, &xfer, 1);
  273. /*
  274. * We aren't using spi_write_then_read() because we need to be able
  275. * to get hold of the effective_speed_hz from the xfer
  276. */
  277. ret = spi_sync(priv->spi, &msg);
  278. if (ret) {
  279. dev_err_ratelimited(&priv->spi->dev, "SPI transfer failed %pe\n",
  280. ERR_PTR(ret));
  281. goto free_bufs;
  282. }
  283. if (effective_speed_hz)
  284. *effective_speed_hz = xfer.effective_speed_hz;
  285. for (i = 0; i < max_count - count_skip; i++) {
  286. val = tsc2046_adc_get_value(&rx_buf[count_skip + i]);
  287. val_normalized += val;
  288. }
  289. ret = DIV_ROUND_UP(val_normalized, max_count - count_skip);
  290. free_bufs:
  291. kfree(rx_buf);
  292. free_tx:
  293. kfree(tx_buf);
  294. return ret;
  295. }
  296. static size_t tsc2046_adc_group_set_layout(struct tsc2046_adc_priv *priv,
  297. unsigned int group,
  298. unsigned int ch_idx)
  299. {
  300. struct tsc2046_adc_ch_cfg *ch = &priv->ch_cfg[ch_idx];
  301. struct tsc2046_adc_group_layout *cur;
  302. unsigned int max_count, count_skip;
  303. unsigned int offset = 0;
  304. if (group)
  305. offset = priv->l[group - 1].offset + priv->l[group - 1].count;
  306. count_skip = tsc2046_adc_time_to_count(priv, ch->settling_time_us);
  307. max_count = count_skip + ch->oversampling_ratio;
  308. cur = &priv->l[group];
  309. cur->offset = offset;
  310. cur->count = max_count;
  311. cur->skip = count_skip;
  312. return sizeof(*priv->tx) * max_count;
  313. }
  314. static void tsc2046_adc_group_set_cmd(struct tsc2046_adc_priv *priv,
  315. unsigned int group, int ch_idx)
  316. {
  317. struct tsc2046_adc_group_layout *l = &priv->l[group];
  318. unsigned int i;
  319. u8 cmd;
  320. /*
  321. * Do not enable automatic power down on working samples. Otherwise the
  322. * plates will never be completely charged.
  323. */
  324. cmd = tsc2046_adc_get_cmd(priv, ch_idx, true);
  325. for (i = 0; i < l->count - 1; i++)
  326. priv->tx[l->offset + i].cmd = cmd;
  327. /* automatically power down on last sample */
  328. priv->tx[l->offset + i].cmd = tsc2046_adc_get_cmd(priv, ch_idx, false);
  329. }
  330. static u16 tsc2046_adc_get_val(struct tsc2046_adc_priv *priv, int group)
  331. {
  332. struct tsc2046_adc_group_layout *l;
  333. unsigned int val, val_normalized = 0;
  334. int valid_count, i;
  335. l = &priv->l[group];
  336. valid_count = l->count - l->skip;
  337. for (i = 0; i < valid_count; i++) {
  338. val = tsc2046_adc_get_value(&priv->rx[l->offset + l->skip + i]);
  339. val_normalized += val;
  340. }
  341. return DIV_ROUND_UP(val_normalized, valid_count);
  342. }
  343. static int tsc2046_adc_scan(struct iio_dev *indio_dev)
  344. {
  345. struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
  346. struct device *dev = &priv->spi->dev;
  347. int group;
  348. int ret;
  349. ret = spi_sync(priv->spi, &priv->msg);
  350. if (ret < 0) {
  351. dev_err_ratelimited(dev, "SPI transfer failed: %pe\n", ERR_PTR(ret));
  352. return ret;
  353. }
  354. for (group = 0; group < priv->groups; group++)
  355. priv->scan_buf.data[group] = tsc2046_adc_get_val(priv, group);
  356. ret = iio_push_to_buffers_with_timestamp(indio_dev, &priv->scan_buf,
  357. iio_get_time_ns(indio_dev));
  358. /* If the consumer is kfifo, we may get a EBUSY here - ignore it. */
  359. if (ret < 0 && ret != -EBUSY) {
  360. dev_err_ratelimited(dev, "Failed to push scan buffer %pe\n",
  361. ERR_PTR(ret));
  362. return ret;
  363. }
  364. return 0;
  365. }
  366. static irqreturn_t tsc2046_adc_trigger_handler(int irq, void *p)
  367. {
  368. struct iio_poll_func *pf = p;
  369. struct iio_dev *indio_dev = pf->indio_dev;
  370. struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
  371. mutex_lock(&priv->slock);
  372. tsc2046_adc_scan(indio_dev);
  373. mutex_unlock(&priv->slock);
  374. iio_trigger_notify_done(indio_dev->trig);
  375. return IRQ_HANDLED;
  376. }
  377. static int tsc2046_adc_read_raw(struct iio_dev *indio_dev,
  378. struct iio_chan_spec const *chan,
  379. int *val, int *val2, long m)
  380. {
  381. struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
  382. int ret;
  383. switch (m) {
  384. case IIO_CHAN_INFO_RAW:
  385. ret = tsc2046_adc_read_one(priv, chan->channel, NULL);
  386. if (ret < 0)
  387. return ret;
  388. *val = ret;
  389. return IIO_VAL_INT;
  390. case IIO_CHAN_INFO_SCALE:
  391. /*
  392. * Note: the TSC2046 has internal voltage divider on the VBAT
  393. * line. This divider can be influenced by external divider.
  394. * So, it is better to use external voltage-divider driver
  395. * instead, which is calculating complete chain.
  396. */
  397. *val = priv->vref_mv;
  398. *val2 = chan->scan_type.realbits;
  399. return IIO_VAL_FRACTIONAL_LOG2;
  400. }
  401. return -EINVAL;
  402. }
  403. static int tsc2046_adc_update_scan_mode(struct iio_dev *indio_dev,
  404. const unsigned long *active_scan_mask)
  405. {
  406. struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
  407. unsigned int ch_idx, group = 0;
  408. size_t size;
  409. mutex_lock(&priv->slock);
  410. size = 0;
  411. for_each_set_bit(ch_idx, active_scan_mask, ARRAY_SIZE(priv->l)) {
  412. size += tsc2046_adc_group_set_layout(priv, group, ch_idx);
  413. tsc2046_adc_group_set_cmd(priv, group, ch_idx);
  414. group++;
  415. }
  416. priv->groups = group;
  417. priv->xfer.len = size;
  418. priv->time_per_scan_us = size * 8 * priv->time_per_bit_ns / NSEC_PER_USEC;
  419. if (priv->scan_interval_us < priv->time_per_scan_us)
  420. dev_warn(&priv->spi->dev, "The scan interval (%d) is less then calculated scan time (%d)\n",
  421. priv->scan_interval_us, priv->time_per_scan_us);
  422. mutex_unlock(&priv->slock);
  423. return 0;
  424. }
  425. static const struct iio_info tsc2046_adc_info = {
  426. .read_raw = tsc2046_adc_read_raw,
  427. .update_scan_mode = tsc2046_adc_update_scan_mode,
  428. };
  429. static enum hrtimer_restart tsc2046_adc_timer(struct hrtimer *hrtimer)
  430. {
  431. struct tsc2046_adc_priv *priv = container_of(hrtimer,
  432. struct tsc2046_adc_priv,
  433. trig_timer);
  434. unsigned long flags;
  435. /*
  436. * This state machine should address following challenges :
  437. * - the interrupt source is based on level shifter attached to the X
  438. * channel of ADC. It will change the state every time we switch
  439. * between channels. So, we need to disable IRQ if we do
  440. * iio_trigger_poll().
  441. * - we should do iio_trigger_poll() at some reduced sample rate
  442. * - we should still trigger for some amount of time after last
  443. * interrupt with enabled IRQ was processed.
  444. */
  445. spin_lock_irqsave(&priv->state_lock, flags);
  446. switch (priv->state) {
  447. case TSC2046_STATE_ENABLE_IRQ:
  448. if (priv->poll_cnt < TI_TSC2046_POLL_CNT) {
  449. priv->poll_cnt++;
  450. hrtimer_start(&priv->trig_timer,
  451. ns_to_ktime(priv->scan_interval_us *
  452. NSEC_PER_USEC),
  453. HRTIMER_MODE_REL_SOFT);
  454. if (priv->poll_cnt >= TI_TSC2046_MIN_POLL_CNT) {
  455. priv->state = TSC2046_STATE_POLL_IRQ_DISABLE;
  456. enable_irq(priv->spi->irq);
  457. } else {
  458. priv->state = TSC2046_STATE_POLL;
  459. }
  460. } else {
  461. priv->state = TSC2046_STATE_STANDBY;
  462. enable_irq(priv->spi->irq);
  463. }
  464. break;
  465. case TSC2046_STATE_POLL_IRQ_DISABLE:
  466. disable_irq_nosync(priv->spi->irq);
  467. fallthrough;
  468. case TSC2046_STATE_POLL:
  469. priv->state = TSC2046_STATE_ENABLE_IRQ;
  470. /* iio_trigger_poll() starts hrtimer */
  471. iio_trigger_poll(priv->trig);
  472. break;
  473. case TSC2046_STATE_SHUTDOWN:
  474. break;
  475. case TSC2046_STATE_STANDBY:
  476. fallthrough;
  477. default:
  478. dev_warn(&priv->spi->dev, "Got unexpected state: %i\n",
  479. priv->state);
  480. break;
  481. }
  482. spin_unlock_irqrestore(&priv->state_lock, flags);
  483. return HRTIMER_NORESTART;
  484. }
  485. static irqreturn_t tsc2046_adc_irq(int irq, void *dev_id)
  486. {
  487. struct iio_dev *indio_dev = dev_id;
  488. struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
  489. unsigned long flags;
  490. hrtimer_try_to_cancel(&priv->trig_timer);
  491. spin_lock_irqsave(&priv->state_lock, flags);
  492. if (priv->state != TSC2046_STATE_SHUTDOWN) {
  493. priv->state = TSC2046_STATE_ENABLE_IRQ;
  494. priv->poll_cnt = 0;
  495. /* iio_trigger_poll() starts hrtimer */
  496. disable_irq_nosync(priv->spi->irq);
  497. iio_trigger_poll(priv->trig);
  498. }
  499. spin_unlock_irqrestore(&priv->state_lock, flags);
  500. return IRQ_HANDLED;
  501. }
  502. static void tsc2046_adc_reenable_trigger(struct iio_trigger *trig)
  503. {
  504. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  505. struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
  506. ktime_t tim;
  507. /*
  508. * We can sample it as fast as we can, but usually we do not need so
  509. * many samples. Reduce the sample rate for default (touchscreen) use
  510. * case.
  511. */
  512. tim = ns_to_ktime((priv->scan_interval_us - priv->time_per_scan_us) *
  513. NSEC_PER_USEC);
  514. hrtimer_start(&priv->trig_timer, tim, HRTIMER_MODE_REL_SOFT);
  515. }
  516. static int tsc2046_adc_set_trigger_state(struct iio_trigger *trig, bool enable)
  517. {
  518. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  519. struct tsc2046_adc_priv *priv = iio_priv(indio_dev);
  520. unsigned long flags;
  521. if (enable) {
  522. spin_lock_irqsave(&priv->state_lock, flags);
  523. if (priv->state == TSC2046_STATE_SHUTDOWN) {
  524. priv->state = TSC2046_STATE_STANDBY;
  525. enable_irq(priv->spi->irq);
  526. }
  527. spin_unlock_irqrestore(&priv->state_lock, flags);
  528. } else {
  529. spin_lock_irqsave(&priv->state_lock, flags);
  530. if (priv->state == TSC2046_STATE_STANDBY ||
  531. priv->state == TSC2046_STATE_POLL_IRQ_DISABLE)
  532. disable_irq_nosync(priv->spi->irq);
  533. priv->state = TSC2046_STATE_SHUTDOWN;
  534. spin_unlock_irqrestore(&priv->state_lock, flags);
  535. hrtimer_cancel(&priv->trig_timer);
  536. }
  537. return 0;
  538. }
  539. static const struct iio_trigger_ops tsc2046_adc_trigger_ops = {
  540. .set_trigger_state = tsc2046_adc_set_trigger_state,
  541. .reenable = tsc2046_adc_reenable_trigger,
  542. };
  543. static int tsc2046_adc_setup_spi_msg(struct tsc2046_adc_priv *priv)
  544. {
  545. unsigned int ch_idx;
  546. size_t size;
  547. int ret;
  548. /*
  549. * Make dummy read to set initial power state and get real SPI clock
  550. * freq. It seems to be not important which channel is used for this
  551. * case.
  552. */
  553. ret = tsc2046_adc_read_one(priv, TI_TSC2046_ADDR_TEMP0,
  554. &priv->effective_speed_hz);
  555. if (ret < 0)
  556. return ret;
  557. /*
  558. * In case SPI controller do not report effective_speed_hz, use
  559. * configure value and hope it will match.
  560. */
  561. if (!priv->effective_speed_hz)
  562. priv->effective_speed_hz = priv->spi->max_speed_hz;
  563. priv->scan_interval_us = TI_TSC2046_SAMPLE_INTERVAL_US;
  564. priv->time_per_bit_ns = DIV_ROUND_UP(NSEC_PER_SEC,
  565. priv->effective_speed_hz);
  566. /*
  567. * Calculate and allocate maximal size buffer if all channels are
  568. * enabled.
  569. */
  570. size = 0;
  571. for (ch_idx = 0; ch_idx < ARRAY_SIZE(priv->l); ch_idx++)
  572. size += tsc2046_adc_group_set_layout(priv, ch_idx, ch_idx);
  573. if (size > PAGE_SIZE) {
  574. dev_err(&priv->spi->dev,
  575. "Calculated scan buffer is too big. Try to reduce spi-max-frequency, settling-time-us or oversampling-ratio\n");
  576. return -ENOSPC;
  577. }
  578. priv->tx = devm_kzalloc(&priv->spi->dev, size, GFP_KERNEL);
  579. if (!priv->tx)
  580. return -ENOMEM;
  581. priv->rx = devm_kzalloc(&priv->spi->dev, size, GFP_KERNEL);
  582. if (!priv->rx)
  583. return -ENOMEM;
  584. priv->xfer.tx_buf = priv->tx;
  585. priv->xfer.rx_buf = priv->rx;
  586. priv->xfer.len = size;
  587. spi_message_init_with_transfers(&priv->msg, &priv->xfer, 1);
  588. return 0;
  589. }
  590. static void tsc2046_adc_parse_fwnode(struct tsc2046_adc_priv *priv)
  591. {
  592. struct fwnode_handle *child;
  593. struct device *dev = &priv->spi->dev;
  594. unsigned int i;
  595. for (i = 0; i < ARRAY_SIZE(priv->ch_cfg); i++) {
  596. priv->ch_cfg[i].settling_time_us = 1;
  597. priv->ch_cfg[i].oversampling_ratio = 1;
  598. }
  599. device_for_each_child_node(dev, child) {
  600. u32 stl, overs, reg;
  601. int ret;
  602. ret = fwnode_property_read_u32(child, "reg", &reg);
  603. if (ret) {
  604. dev_err(dev, "invalid reg on %pfw, err: %pe\n", child,
  605. ERR_PTR(ret));
  606. continue;
  607. }
  608. if (reg >= ARRAY_SIZE(priv->ch_cfg)) {
  609. dev_err(dev, "%pfw: Unsupported reg value: %i, max supported is: %zu.\n",
  610. child, reg, ARRAY_SIZE(priv->ch_cfg));
  611. continue;
  612. }
  613. ret = fwnode_property_read_u32(child, "settling-time-us", &stl);
  614. if (!ret)
  615. priv->ch_cfg[reg].settling_time_us = stl;
  616. ret = fwnode_property_read_u32(child, "oversampling-ratio",
  617. &overs);
  618. if (!ret)
  619. priv->ch_cfg[reg].oversampling_ratio = overs;
  620. }
  621. }
  622. static void tsc2046_adc_regulator_disable(void *data)
  623. {
  624. struct tsc2046_adc_priv *priv = data;
  625. regulator_disable(priv->vref_reg);
  626. }
  627. static int tsc2046_adc_configure_regulator(struct tsc2046_adc_priv *priv)
  628. {
  629. struct device *dev = &priv->spi->dev;
  630. int ret;
  631. priv->vref_reg = devm_regulator_get_optional(dev, "vref");
  632. if (IS_ERR(priv->vref_reg)) {
  633. /* If regulator exists but can't be get, return an error */
  634. if (PTR_ERR(priv->vref_reg) != -ENODEV)
  635. return PTR_ERR(priv->vref_reg);
  636. priv->vref_reg = NULL;
  637. }
  638. if (!priv->vref_reg) {
  639. /* Use internal reference */
  640. priv->vref_mv = TI_TSC2046_INT_VREF;
  641. return 0;
  642. }
  643. ret = regulator_enable(priv->vref_reg);
  644. if (ret)
  645. return ret;
  646. ret = devm_add_action_or_reset(dev, tsc2046_adc_regulator_disable,
  647. priv);
  648. if (ret)
  649. return ret;
  650. ret = regulator_get_voltage(priv->vref_reg);
  651. if (ret < 0)
  652. return ret;
  653. priv->vref_mv = ret / MILLI;
  654. return 0;
  655. }
  656. static int tsc2046_adc_probe(struct spi_device *spi)
  657. {
  658. const struct tsc2046_adc_dcfg *dcfg;
  659. struct device *dev = &spi->dev;
  660. struct tsc2046_adc_priv *priv;
  661. struct iio_dev *indio_dev;
  662. struct iio_trigger *trig;
  663. int ret;
  664. if (spi->max_speed_hz > TI_TSC2046_MAX_CLK_FREQ) {
  665. dev_err(dev, "SPI max_speed_hz is too high: %d Hz. Max supported freq is %zu Hz\n",
  666. spi->max_speed_hz, TI_TSC2046_MAX_CLK_FREQ);
  667. return -EINVAL;
  668. }
  669. dcfg = device_get_match_data(dev);
  670. if (!dcfg) {
  671. const struct spi_device_id *id = spi_get_device_id(spi);
  672. dcfg = (const struct tsc2046_adc_dcfg *)id->driver_data;
  673. }
  674. if (!dcfg)
  675. return -EINVAL;
  676. spi->bits_per_word = 8;
  677. spi->mode &= ~SPI_MODE_X_MASK;
  678. spi->mode |= SPI_MODE_0;
  679. ret = spi_setup(spi);
  680. if (ret < 0)
  681. return dev_err_probe(dev, ret, "Error in SPI setup\n");
  682. indio_dev = devm_iio_device_alloc(dev, sizeof(*priv));
  683. if (!indio_dev)
  684. return -ENOMEM;
  685. priv = iio_priv(indio_dev);
  686. priv->dcfg = dcfg;
  687. priv->spi = spi;
  688. indio_dev->name = TI_TSC2046_NAME;
  689. indio_dev->modes = INDIO_DIRECT_MODE;
  690. indio_dev->channels = dcfg->channels;
  691. indio_dev->num_channels = dcfg->num_channels;
  692. indio_dev->info = &tsc2046_adc_info;
  693. ret = tsc2046_adc_configure_regulator(priv);
  694. if (ret)
  695. return ret;
  696. tsc2046_adc_parse_fwnode(priv);
  697. ret = tsc2046_adc_setup_spi_msg(priv);
  698. if (ret)
  699. return ret;
  700. mutex_init(&priv->slock);
  701. ret = devm_request_irq(dev, spi->irq, &tsc2046_adc_irq,
  702. IRQF_NO_AUTOEN, indio_dev->name, indio_dev);
  703. if (ret)
  704. return ret;
  705. trig = devm_iio_trigger_alloc(dev, "touchscreen-%s", indio_dev->name);
  706. if (!trig)
  707. return -ENOMEM;
  708. priv->trig = trig;
  709. iio_trigger_set_drvdata(trig, indio_dev);
  710. trig->ops = &tsc2046_adc_trigger_ops;
  711. spin_lock_init(&priv->state_lock);
  712. priv->state = TSC2046_STATE_SHUTDOWN;
  713. hrtimer_init(&priv->trig_timer, CLOCK_MONOTONIC,
  714. HRTIMER_MODE_REL_SOFT);
  715. priv->trig_timer.function = tsc2046_adc_timer;
  716. ret = devm_iio_trigger_register(dev, trig);
  717. if (ret) {
  718. dev_err(dev, "failed to register trigger\n");
  719. return ret;
  720. }
  721. ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
  722. &tsc2046_adc_trigger_handler, NULL);
  723. if (ret) {
  724. dev_err(dev, "Failed to setup triggered buffer\n");
  725. return ret;
  726. }
  727. /* set default trigger */
  728. indio_dev->trig = iio_trigger_get(priv->trig);
  729. return devm_iio_device_register(dev, indio_dev);
  730. }
  731. static const struct of_device_id ads7950_of_table[] = {
  732. { .compatible = "ti,tsc2046e-adc", .data = &tsc2046_adc_dcfg_tsc2046e },
  733. { }
  734. };
  735. MODULE_DEVICE_TABLE(of, ads7950_of_table);
  736. static const struct spi_device_id tsc2046_adc_spi_ids[] = {
  737. { "tsc2046e-adc", (unsigned long)&tsc2046_adc_dcfg_tsc2046e },
  738. { }
  739. };
  740. MODULE_DEVICE_TABLE(spi, tsc2046_adc_spi_ids);
  741. static struct spi_driver tsc2046_adc_driver = {
  742. .driver = {
  743. .name = "tsc2046",
  744. .of_match_table = ads7950_of_table,
  745. },
  746. .id_table = tsc2046_adc_spi_ids,
  747. .probe = tsc2046_adc_probe,
  748. };
  749. module_spi_driver(tsc2046_adc_driver);
  750. MODULE_AUTHOR("Oleksij Rempel <[email protected]>");
  751. MODULE_DESCRIPTION("TI TSC2046 ADC");
  752. MODULE_LICENSE("GPL v2");