sx_common.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2021 Google LLC.
  4. *
  5. * Common part of most Semtech SAR sensor.
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/byteorder/generic.h>
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/err.h>
  12. #include <linux/export.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irqreturn.h>
  15. #include <linux/i2c.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/regmap.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <vdso/bits.h>
  21. #include <linux/iio/buffer.h>
  22. #include <linux/iio/events.h>
  23. #include <linux/iio/iio.h>
  24. #include <linux/iio/trigger.h>
  25. #include <linux/iio/triggered_buffer.h>
  26. #include <linux/iio/trigger_consumer.h>
  27. #include "sx_common.h"
  28. /* All Semtech SAR sensors have IRQ bit in the same order. */
  29. #define SX_COMMON_CONVDONE_IRQ BIT(0)
  30. #define SX_COMMON_FAR_IRQ BIT(2)
  31. #define SX_COMMON_CLOSE_IRQ BIT(3)
  32. const struct iio_event_spec sx_common_events[3] = {
  33. {
  34. .type = IIO_EV_TYPE_THRESH,
  35. .dir = IIO_EV_DIR_RISING,
  36. .mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD),
  37. },
  38. {
  39. .type = IIO_EV_TYPE_THRESH,
  40. .dir = IIO_EV_DIR_FALLING,
  41. .mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD),
  42. },
  43. {
  44. .type = IIO_EV_TYPE_THRESH,
  45. .dir = IIO_EV_DIR_EITHER,
  46. .mask_separate = BIT(IIO_EV_INFO_ENABLE) |
  47. BIT(IIO_EV_INFO_HYSTERESIS) |
  48. BIT(IIO_EV_INFO_VALUE),
  49. },
  50. };
  51. EXPORT_SYMBOL_NS_GPL(sx_common_events, "SEMTECH_PROX");
  52. static irqreturn_t sx_common_irq_handler(int irq, void *private)
  53. {
  54. struct iio_dev *indio_dev = private;
  55. struct sx_common_data *data = iio_priv(indio_dev);
  56. if (data->trigger_enabled)
  57. iio_trigger_poll(data->trig);
  58. /*
  59. * Even if no event is enabled, we need to wake the thread to clear the
  60. * interrupt state by reading SX_COMMON_REG_IRQ_SRC.
  61. * It is not possible to do that here because regmap_read takes a mutex.
  62. */
  63. return IRQ_WAKE_THREAD;
  64. }
  65. static void sx_common_push_events(struct iio_dev *indio_dev)
  66. {
  67. int ret;
  68. unsigned int val, chan;
  69. struct sx_common_data *data = iio_priv(indio_dev);
  70. s64 timestamp = iio_get_time_ns(indio_dev);
  71. unsigned long prox_changed;
  72. /* Read proximity state on all channels */
  73. ret = regmap_read(data->regmap, data->chip_info->reg_stat, &val);
  74. if (ret) {
  75. dev_err(&data->client->dev, "i2c transfer error in irq\n");
  76. return;
  77. }
  78. val >>= data->chip_info->stat_offset;
  79. /*
  80. * Only iterate over channels with changes on proximity status that have
  81. * events enabled.
  82. */
  83. prox_changed = (data->chan_prox_stat ^ val) & data->chan_event;
  84. for_each_set_bit(chan, &prox_changed, data->chip_info->num_channels) {
  85. int dir;
  86. u64 ev;
  87. dir = (val & BIT(chan)) ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
  88. ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan,
  89. IIO_EV_TYPE_THRESH, dir);
  90. iio_push_event(indio_dev, ev, timestamp);
  91. }
  92. data->chan_prox_stat = val;
  93. }
  94. static int sx_common_enable_irq(struct sx_common_data *data, unsigned int irq)
  95. {
  96. if (!data->client->irq)
  97. return 0;
  98. return regmap_set_bits(data->regmap, data->chip_info->reg_irq_msk,
  99. irq << data->chip_info->irq_msk_offset);
  100. }
  101. static int sx_common_disable_irq(struct sx_common_data *data, unsigned int irq)
  102. {
  103. if (!data->client->irq)
  104. return 0;
  105. return regmap_clear_bits(data->regmap, data->chip_info->reg_irq_msk,
  106. irq << data->chip_info->irq_msk_offset);
  107. }
  108. static int sx_common_update_chan_en(struct sx_common_data *data,
  109. unsigned long chan_read,
  110. unsigned long chan_event)
  111. {
  112. int ret;
  113. unsigned long channels = chan_read | chan_event;
  114. if ((data->chan_read | data->chan_event) != channels) {
  115. ret = regmap_update_bits(data->regmap,
  116. data->chip_info->reg_enable_chan,
  117. data->chip_info->mask_enable_chan,
  118. channels);
  119. if (ret)
  120. return ret;
  121. }
  122. data->chan_read = chan_read;
  123. data->chan_event = chan_event;
  124. return 0;
  125. }
  126. static int sx_common_get_read_channel(struct sx_common_data *data, int channel)
  127. {
  128. return sx_common_update_chan_en(data, data->chan_read | BIT(channel),
  129. data->chan_event);
  130. }
  131. static int sx_common_put_read_channel(struct sx_common_data *data, int channel)
  132. {
  133. return sx_common_update_chan_en(data, data->chan_read & ~BIT(channel),
  134. data->chan_event);
  135. }
  136. static int sx_common_get_event_channel(struct sx_common_data *data, int channel)
  137. {
  138. return sx_common_update_chan_en(data, data->chan_read,
  139. data->chan_event | BIT(channel));
  140. }
  141. static int sx_common_put_event_channel(struct sx_common_data *data, int channel)
  142. {
  143. return sx_common_update_chan_en(data, data->chan_read,
  144. data->chan_event & ~BIT(channel));
  145. }
  146. /**
  147. * sx_common_read_proximity() - Read raw proximity value.
  148. * @data: Internal data
  149. * @chan: Channel to read
  150. * @val: pointer to return read value.
  151. *
  152. * Request a conversion, wait for the sensor to be ready and
  153. * return the raw proximity value.
  154. */
  155. int sx_common_read_proximity(struct sx_common_data *data,
  156. const struct iio_chan_spec *chan, int *val)
  157. {
  158. int ret;
  159. __be16 rawval;
  160. mutex_lock(&data->mutex);
  161. ret = sx_common_get_read_channel(data, chan->channel);
  162. if (ret)
  163. goto out;
  164. ret = sx_common_enable_irq(data, SX_COMMON_CONVDONE_IRQ);
  165. if (ret)
  166. goto out_put_channel;
  167. mutex_unlock(&data->mutex);
  168. if (data->client->irq) {
  169. ret = wait_for_completion_interruptible(&data->completion);
  170. reinit_completion(&data->completion);
  171. } else {
  172. ret = data->chip_info->ops.wait_for_sample(data);
  173. }
  174. mutex_lock(&data->mutex);
  175. if (ret)
  176. goto out_disable_irq;
  177. ret = data->chip_info->ops.read_prox_data(data, chan, &rawval);
  178. if (ret)
  179. goto out_disable_irq;
  180. *val = sign_extend32(be16_to_cpu(rawval), chan->scan_type.realbits - 1);
  181. ret = sx_common_disable_irq(data, SX_COMMON_CONVDONE_IRQ);
  182. if (ret)
  183. goto out_put_channel;
  184. ret = sx_common_put_read_channel(data, chan->channel);
  185. if (ret)
  186. goto out;
  187. mutex_unlock(&data->mutex);
  188. return IIO_VAL_INT;
  189. out_disable_irq:
  190. sx_common_disable_irq(data, SX_COMMON_CONVDONE_IRQ);
  191. out_put_channel:
  192. sx_common_put_read_channel(data, chan->channel);
  193. out:
  194. mutex_unlock(&data->mutex);
  195. return ret;
  196. }
  197. EXPORT_SYMBOL_NS_GPL(sx_common_read_proximity, "SEMTECH_PROX");
  198. /**
  199. * sx_common_read_event_config() - Configure event setting.
  200. * @indio_dev: iio device object
  201. * @chan: Channel to read
  202. * @type: Type of event (unused)
  203. * @dir: Direction of event (unused)
  204. *
  205. * return if the given channel is used for event gathering.
  206. */
  207. int sx_common_read_event_config(struct iio_dev *indio_dev,
  208. const struct iio_chan_spec *chan,
  209. enum iio_event_type type,
  210. enum iio_event_direction dir)
  211. {
  212. struct sx_common_data *data = iio_priv(indio_dev);
  213. return !!(data->chan_event & BIT(chan->channel));
  214. }
  215. EXPORT_SYMBOL_NS_GPL(sx_common_read_event_config, "SEMTECH_PROX");
  216. /**
  217. * sx_common_write_event_config() - Configure event setting.
  218. * @indio_dev: iio device object
  219. * @chan: Channel to enable
  220. * @type: Type of event (unused)
  221. * @dir: Direction of event (unused)
  222. * @state: State of the event.
  223. *
  224. * Enable/Disable event on a given channel.
  225. */
  226. int sx_common_write_event_config(struct iio_dev *indio_dev,
  227. const struct iio_chan_spec *chan,
  228. enum iio_event_type type,
  229. enum iio_event_direction dir, bool state)
  230. {
  231. struct sx_common_data *data = iio_priv(indio_dev);
  232. unsigned int eventirq = SX_COMMON_FAR_IRQ | SX_COMMON_CLOSE_IRQ;
  233. int ret;
  234. /* If the state hasn't changed, there's nothing to do. */
  235. if (!!(data->chan_event & BIT(chan->channel)) == state)
  236. return 0;
  237. mutex_lock(&data->mutex);
  238. if (state) {
  239. ret = sx_common_get_event_channel(data, chan->channel);
  240. if (ret)
  241. goto out_unlock;
  242. if (!(data->chan_event & ~BIT(chan->channel))) {
  243. ret = sx_common_enable_irq(data, eventirq);
  244. if (ret)
  245. sx_common_put_event_channel(data, chan->channel);
  246. }
  247. } else {
  248. ret = sx_common_put_event_channel(data, chan->channel);
  249. if (ret)
  250. goto out_unlock;
  251. if (!data->chan_event) {
  252. ret = sx_common_disable_irq(data, eventirq);
  253. if (ret)
  254. sx_common_get_event_channel(data, chan->channel);
  255. }
  256. }
  257. out_unlock:
  258. mutex_unlock(&data->mutex);
  259. return ret;
  260. }
  261. EXPORT_SYMBOL_NS_GPL(sx_common_write_event_config, "SEMTECH_PROX");
  262. static int sx_common_set_trigger_state(struct iio_trigger *trig, bool state)
  263. {
  264. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
  265. struct sx_common_data *data = iio_priv(indio_dev);
  266. int ret = 0;
  267. mutex_lock(&data->mutex);
  268. if (state)
  269. ret = sx_common_enable_irq(data, SX_COMMON_CONVDONE_IRQ);
  270. else if (!data->chan_read)
  271. ret = sx_common_disable_irq(data, SX_COMMON_CONVDONE_IRQ);
  272. if (ret)
  273. goto out;
  274. data->trigger_enabled = state;
  275. out:
  276. mutex_unlock(&data->mutex);
  277. return ret;
  278. }
  279. static const struct iio_trigger_ops sx_common_trigger_ops = {
  280. .set_trigger_state = sx_common_set_trigger_state,
  281. };
  282. static irqreturn_t sx_common_irq_thread_handler(int irq, void *private)
  283. {
  284. struct iio_dev *indio_dev = private;
  285. struct sx_common_data *data = iio_priv(indio_dev);
  286. int ret;
  287. unsigned int val;
  288. mutex_lock(&data->mutex);
  289. ret = regmap_read(data->regmap, SX_COMMON_REG_IRQ_SRC, &val);
  290. if (ret) {
  291. dev_err(&data->client->dev, "i2c transfer error in irq\n");
  292. goto out;
  293. }
  294. if (val & ((SX_COMMON_FAR_IRQ | SX_COMMON_CLOSE_IRQ) << data->chip_info->irq_msk_offset))
  295. sx_common_push_events(indio_dev);
  296. if (val & (SX_COMMON_CONVDONE_IRQ << data->chip_info->irq_msk_offset))
  297. complete(&data->completion);
  298. out:
  299. mutex_unlock(&data->mutex);
  300. return IRQ_HANDLED;
  301. }
  302. static irqreturn_t sx_common_trigger_handler(int irq, void *private)
  303. {
  304. struct iio_poll_func *pf = private;
  305. struct iio_dev *indio_dev = pf->indio_dev;
  306. struct sx_common_data *data = iio_priv(indio_dev);
  307. __be16 val;
  308. int bit, ret, i = 0;
  309. mutex_lock(&data->mutex);
  310. iio_for_each_active_channel(indio_dev, bit) {
  311. ret = data->chip_info->ops.read_prox_data(data,
  312. &indio_dev->channels[bit],
  313. &val);
  314. if (ret)
  315. goto out;
  316. data->buffer.channels[i++] = val;
  317. }
  318. iio_push_to_buffers_with_ts(indio_dev, &data->buffer,
  319. sizeof(data->buffer), pf->timestamp);
  320. out:
  321. mutex_unlock(&data->mutex);
  322. iio_trigger_notify_done(indio_dev->trig);
  323. return IRQ_HANDLED;
  324. }
  325. static int sx_common_buffer_preenable(struct iio_dev *indio_dev)
  326. {
  327. struct sx_common_data *data = iio_priv(indio_dev);
  328. unsigned long channels = 0;
  329. int bit, ret;
  330. mutex_lock(&data->mutex);
  331. iio_for_each_active_channel(indio_dev, bit)
  332. __set_bit(indio_dev->channels[bit].channel, &channels);
  333. ret = sx_common_update_chan_en(data, channels, data->chan_event);
  334. mutex_unlock(&data->mutex);
  335. return ret;
  336. }
  337. static int sx_common_buffer_postdisable(struct iio_dev *indio_dev)
  338. {
  339. struct sx_common_data *data = iio_priv(indio_dev);
  340. int ret;
  341. mutex_lock(&data->mutex);
  342. ret = sx_common_update_chan_en(data, 0, data->chan_event);
  343. mutex_unlock(&data->mutex);
  344. return ret;
  345. }
  346. static const struct iio_buffer_setup_ops sx_common_buffer_setup_ops = {
  347. .preenable = sx_common_buffer_preenable,
  348. .postdisable = sx_common_buffer_postdisable,
  349. };
  350. #define SX_COMMON_SOFT_RESET 0xde
  351. static int sx_common_init_device(struct device *dev, struct iio_dev *indio_dev)
  352. {
  353. struct sx_common_data *data = iio_priv(indio_dev);
  354. struct sx_common_reg_default tmp;
  355. const struct sx_common_reg_default *initval;
  356. int ret;
  357. unsigned int i, val;
  358. ret = regmap_write(data->regmap, data->chip_info->reg_reset,
  359. SX_COMMON_SOFT_RESET);
  360. if (ret)
  361. return ret;
  362. usleep_range(1000, 2000); /* power-up time is ~1ms. */
  363. /* Clear reset interrupt state by reading SX_COMMON_REG_IRQ_SRC. */
  364. ret = regmap_read(data->regmap, SX_COMMON_REG_IRQ_SRC, &val);
  365. if (ret)
  366. return ret;
  367. /* Program defaults from constant or BIOS. */
  368. for (i = 0; i < data->chip_info->num_default_regs; i++) {
  369. initval = data->chip_info->ops.get_default_reg(dev, i, &tmp);
  370. ret = regmap_write(data->regmap, initval->reg, initval->def);
  371. if (ret)
  372. return ret;
  373. }
  374. return data->chip_info->ops.init_compensation(indio_dev);
  375. }
  376. /**
  377. * sx_common_probe() - Common setup for Semtech SAR sensor
  378. * @client: I2C client object
  379. * @chip_info: Semtech sensor chip information.
  380. * @regmap_config: Sensor registers map configuration.
  381. */
  382. int sx_common_probe(struct i2c_client *client,
  383. const struct sx_common_chip_info *chip_info,
  384. const struct regmap_config *regmap_config)
  385. {
  386. static const char * const regulator_names[] = { "vdd", "svdd" };
  387. struct device *dev = &client->dev;
  388. struct iio_dev *indio_dev;
  389. struct sx_common_data *data;
  390. int ret;
  391. indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
  392. if (!indio_dev)
  393. return -ENOMEM;
  394. data = iio_priv(indio_dev);
  395. data->chip_info = chip_info;
  396. data->client = client;
  397. mutex_init(&data->mutex);
  398. init_completion(&data->completion);
  399. data->regmap = devm_regmap_init_i2c(client, regmap_config);
  400. if (IS_ERR(data->regmap))
  401. return dev_err_probe(dev, PTR_ERR(data->regmap),
  402. "Could init register map\n");
  403. ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulator_names),
  404. regulator_names);
  405. if (ret)
  406. return dev_err_probe(dev, ret, "Unable to get regulators\n");
  407. /* Must wait for Tpor time after initial power up */
  408. usleep_range(1000, 1100);
  409. ret = data->chip_info->ops.check_whoami(dev, indio_dev);
  410. if (ret)
  411. return dev_err_probe(dev, ret, "error reading WHOAMI\n");
  412. indio_dev->modes = INDIO_DIRECT_MODE;
  413. indio_dev->channels = data->chip_info->iio_channels;
  414. indio_dev->num_channels = data->chip_info->num_iio_channels;
  415. indio_dev->info = &data->chip_info->iio_info;
  416. i2c_set_clientdata(client, indio_dev);
  417. ret = sx_common_init_device(dev, indio_dev);
  418. if (ret)
  419. return dev_err_probe(dev, ret, "Unable to initialize sensor\n");
  420. if (client->irq) {
  421. ret = devm_request_threaded_irq(dev, client->irq,
  422. sx_common_irq_handler,
  423. sx_common_irq_thread_handler,
  424. IRQF_ONESHOT,
  425. "sx_event", indio_dev);
  426. if (ret)
  427. return dev_err_probe(dev, ret, "No IRQ\n");
  428. data->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
  429. indio_dev->name,
  430. iio_device_id(indio_dev));
  431. if (!data->trig)
  432. return -ENOMEM;
  433. data->trig->ops = &sx_common_trigger_ops;
  434. iio_trigger_set_drvdata(data->trig, indio_dev);
  435. ret = devm_iio_trigger_register(dev, data->trig);
  436. if (ret)
  437. return ret;
  438. }
  439. ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
  440. iio_pollfunc_store_time,
  441. sx_common_trigger_handler,
  442. &sx_common_buffer_setup_ops);
  443. if (ret)
  444. return ret;
  445. return devm_iio_device_register(dev, indio_dev);
  446. }
  447. EXPORT_SYMBOL_NS_GPL(sx_common_probe, "SEMTECH_PROX");
  448. MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>");
  449. MODULE_DESCRIPTION("Common functions and structures for Semtech sensor");
  450. MODULE_LICENSE("GPL v2");