ti-tsc2046.c 23 KB

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