ti-ads1015.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /*
  2. * ADS1015 - Texas Instruments Analog-to-Digital Converter
  3. *
  4. * Copyright (c) 2016, Intel Corporation.
  5. *
  6. * This file is subject to the terms and conditions of version 2 of
  7. * the GNU General Public License. See the file COPYING in the main
  8. * directory of this archive for more details.
  9. *
  10. * IIO driver for ADS1015 ADC 7-bit I2C slave address:
  11. * * 0x48 - ADDR connected to Ground
  12. * * 0x49 - ADDR connected to Vdd
  13. * * 0x4A - ADDR connected to SDA
  14. * * 0x4B - ADDR connected to SCL
  15. */
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/i2c.h>
  19. #include <linux/regmap.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/mutex.h>
  22. #include <linux/delay.h>
  23. #include <linux/i2c/ads1015.h>
  24. #include <linux/iio/iio.h>
  25. #include <linux/iio/types.h>
  26. #include <linux/iio/sysfs.h>
  27. #include <linux/iio/buffer.h>
  28. #include <linux/iio/triggered_buffer.h>
  29. #include <linux/iio/trigger_consumer.h>
  30. #define ADS1015_DRV_NAME "ads1015"
  31. #define ADS1015_CONV_REG 0x00
  32. #define ADS1015_CFG_REG 0x01
  33. #define ADS1015_CFG_DR_SHIFT 5
  34. #define ADS1015_CFG_MOD_SHIFT 8
  35. #define ADS1015_CFG_PGA_SHIFT 9
  36. #define ADS1015_CFG_MUX_SHIFT 12
  37. #define ADS1015_CFG_DR_MASK GENMASK(7, 5)
  38. #define ADS1015_CFG_MOD_MASK BIT(8)
  39. #define ADS1015_CFG_PGA_MASK GENMASK(11, 9)
  40. #define ADS1015_CFG_MUX_MASK GENMASK(14, 12)
  41. /* device operating modes */
  42. #define ADS1015_CONTINUOUS 0
  43. #define ADS1015_SINGLESHOT 1
  44. #define ADS1015_SLEEP_DELAY_MS 2000
  45. #define ADS1015_DEFAULT_PGA 2
  46. #define ADS1015_DEFAULT_DATA_RATE 4
  47. #define ADS1015_DEFAULT_CHAN 0
  48. enum {
  49. ADS1015,
  50. ADS1115,
  51. };
  52. enum ads1015_channels {
  53. ADS1015_AIN0_AIN1 = 0,
  54. ADS1015_AIN0_AIN3,
  55. ADS1015_AIN1_AIN3,
  56. ADS1015_AIN2_AIN3,
  57. ADS1015_AIN0,
  58. ADS1015_AIN1,
  59. ADS1015_AIN2,
  60. ADS1015_AIN3,
  61. ADS1015_TIMESTAMP,
  62. };
  63. static const unsigned int ads1015_data_rate[] = {
  64. 128, 250, 490, 920, 1600, 2400, 3300, 3300
  65. };
  66. static const unsigned int ads1115_data_rate[] = {
  67. 8, 16, 32, 64, 128, 250, 475, 860
  68. };
  69. static const struct {
  70. int scale;
  71. int uscale;
  72. } ads1015_scale[] = {
  73. {3, 0},
  74. {2, 0},
  75. {1, 0},
  76. {0, 500000},
  77. {0, 250000},
  78. {0, 125000},
  79. {0, 125000},
  80. {0, 125000},
  81. };
  82. #define ADS1015_V_CHAN(_chan, _addr) { \
  83. .type = IIO_VOLTAGE, \
  84. .indexed = 1, \
  85. .address = _addr, \
  86. .channel = _chan, \
  87. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  88. BIT(IIO_CHAN_INFO_SCALE) | \
  89. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  90. .scan_index = _addr, \
  91. .scan_type = { \
  92. .sign = 's', \
  93. .realbits = 12, \
  94. .storagebits = 16, \
  95. .shift = 4, \
  96. .endianness = IIO_CPU, \
  97. }, \
  98. .datasheet_name = "AIN"#_chan, \
  99. }
  100. #define ADS1015_V_DIFF_CHAN(_chan, _chan2, _addr) { \
  101. .type = IIO_VOLTAGE, \
  102. .differential = 1, \
  103. .indexed = 1, \
  104. .address = _addr, \
  105. .channel = _chan, \
  106. .channel2 = _chan2, \
  107. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  108. BIT(IIO_CHAN_INFO_SCALE) | \
  109. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  110. .scan_index = _addr, \
  111. .scan_type = { \
  112. .sign = 's', \
  113. .realbits = 12, \
  114. .storagebits = 16, \
  115. .shift = 4, \
  116. .endianness = IIO_CPU, \
  117. }, \
  118. .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \
  119. }
  120. #define ADS1115_V_CHAN(_chan, _addr) { \
  121. .type = IIO_VOLTAGE, \
  122. .indexed = 1, \
  123. .address = _addr, \
  124. .channel = _chan, \
  125. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  126. BIT(IIO_CHAN_INFO_SCALE) | \
  127. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  128. .scan_index = _addr, \
  129. .scan_type = { \
  130. .sign = 's', \
  131. .realbits = 16, \
  132. .storagebits = 16, \
  133. .endianness = IIO_CPU, \
  134. }, \
  135. .datasheet_name = "AIN"#_chan, \
  136. }
  137. #define ADS1115_V_DIFF_CHAN(_chan, _chan2, _addr) { \
  138. .type = IIO_VOLTAGE, \
  139. .differential = 1, \
  140. .indexed = 1, \
  141. .address = _addr, \
  142. .channel = _chan, \
  143. .channel2 = _chan2, \
  144. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
  145. BIT(IIO_CHAN_INFO_SCALE) | \
  146. BIT(IIO_CHAN_INFO_SAMP_FREQ), \
  147. .scan_index = _addr, \
  148. .scan_type = { \
  149. .sign = 's', \
  150. .realbits = 16, \
  151. .storagebits = 16, \
  152. .endianness = IIO_CPU, \
  153. }, \
  154. .datasheet_name = "AIN"#_chan"-AIN"#_chan2, \
  155. }
  156. struct ads1015_data {
  157. struct regmap *regmap;
  158. /*
  159. * Protects ADC ops, e.g: concurrent sysfs/buffered
  160. * data reads, configuration updates
  161. */
  162. struct mutex lock;
  163. struct ads1015_channel_data channel_data[ADS1015_CHANNELS];
  164. unsigned int *data_rate;
  165. };
  166. static bool ads1015_is_writeable_reg(struct device *dev, unsigned int reg)
  167. {
  168. return (reg == ADS1015_CFG_REG);
  169. }
  170. static const struct regmap_config ads1015_regmap_config = {
  171. .reg_bits = 8,
  172. .val_bits = 16,
  173. .max_register = ADS1015_CFG_REG,
  174. .writeable_reg = ads1015_is_writeable_reg,
  175. };
  176. static const struct iio_chan_spec ads1015_channels[] = {
  177. ADS1015_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1),
  178. ADS1015_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3),
  179. ADS1015_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3),
  180. ADS1015_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3),
  181. ADS1015_V_CHAN(0, ADS1015_AIN0),
  182. ADS1015_V_CHAN(1, ADS1015_AIN1),
  183. ADS1015_V_CHAN(2, ADS1015_AIN2),
  184. ADS1015_V_CHAN(3, ADS1015_AIN3),
  185. IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP),
  186. };
  187. static const struct iio_chan_spec ads1115_channels[] = {
  188. ADS1115_V_DIFF_CHAN(0, 1, ADS1015_AIN0_AIN1),
  189. ADS1115_V_DIFF_CHAN(0, 3, ADS1015_AIN0_AIN3),
  190. ADS1115_V_DIFF_CHAN(1, 3, ADS1015_AIN1_AIN3),
  191. ADS1115_V_DIFF_CHAN(2, 3, ADS1015_AIN2_AIN3),
  192. ADS1115_V_CHAN(0, ADS1015_AIN0),
  193. ADS1115_V_CHAN(1, ADS1015_AIN1),
  194. ADS1115_V_CHAN(2, ADS1015_AIN2),
  195. ADS1115_V_CHAN(3, ADS1015_AIN3),
  196. IIO_CHAN_SOFT_TIMESTAMP(ADS1015_TIMESTAMP),
  197. };
  198. static int ads1015_set_power_state(struct ads1015_data *data, bool on)
  199. {
  200. int ret;
  201. struct device *dev = regmap_get_device(data->regmap);
  202. if (on) {
  203. ret = pm_runtime_get_sync(dev);
  204. if (ret < 0)
  205. pm_runtime_put_noidle(dev);
  206. } else {
  207. pm_runtime_mark_last_busy(dev);
  208. ret = pm_runtime_put_autosuspend(dev);
  209. }
  210. return ret;
  211. }
  212. static
  213. int ads1015_get_adc_result(struct ads1015_data *data, int chan, int *val)
  214. {
  215. int ret, pga, dr, conv_time;
  216. bool change;
  217. if (chan < 0 || chan >= ADS1015_CHANNELS)
  218. return -EINVAL;
  219. pga = data->channel_data[chan].pga;
  220. dr = data->channel_data[chan].data_rate;
  221. ret = regmap_update_bits_check(data->regmap, ADS1015_CFG_REG,
  222. ADS1015_CFG_MUX_MASK |
  223. ADS1015_CFG_PGA_MASK,
  224. chan << ADS1015_CFG_MUX_SHIFT |
  225. pga << ADS1015_CFG_PGA_SHIFT,
  226. &change);
  227. if (ret < 0)
  228. return ret;
  229. if (change) {
  230. conv_time = DIV_ROUND_UP(USEC_PER_SEC, data->data_rate[dr]);
  231. usleep_range(conv_time, conv_time + 1);
  232. }
  233. return regmap_read(data->regmap, ADS1015_CONV_REG, val);
  234. }
  235. static irqreturn_t ads1015_trigger_handler(int irq, void *p)
  236. {
  237. struct iio_poll_func *pf = p;
  238. struct iio_dev *indio_dev = pf->indio_dev;
  239. struct ads1015_data *data = iio_priv(indio_dev);
  240. s16 buf[8]; /* 1x s16 ADC val + 3x s16 padding + 4x s16 timestamp */
  241. int chan, ret, res;
  242. memset(buf, 0, sizeof(buf));
  243. mutex_lock(&data->lock);
  244. chan = find_first_bit(indio_dev->active_scan_mask,
  245. indio_dev->masklength);
  246. ret = ads1015_get_adc_result(data, chan, &res);
  247. if (ret < 0) {
  248. mutex_unlock(&data->lock);
  249. goto err;
  250. }
  251. buf[0] = res;
  252. mutex_unlock(&data->lock);
  253. iio_push_to_buffers_with_timestamp(indio_dev, buf,
  254. iio_get_time_ns(indio_dev));
  255. err:
  256. iio_trigger_notify_done(indio_dev->trig);
  257. return IRQ_HANDLED;
  258. }
  259. static int ads1015_set_scale(struct ads1015_data *data, int chan,
  260. int scale, int uscale)
  261. {
  262. int i, ret, rindex = -1;
  263. for (i = 0; i < ARRAY_SIZE(ads1015_scale); i++)
  264. if (ads1015_scale[i].scale == scale &&
  265. ads1015_scale[i].uscale == uscale) {
  266. rindex = i;
  267. break;
  268. }
  269. if (rindex < 0)
  270. return -EINVAL;
  271. ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG,
  272. ADS1015_CFG_PGA_MASK,
  273. rindex << ADS1015_CFG_PGA_SHIFT);
  274. if (ret < 0)
  275. return ret;
  276. data->channel_data[chan].pga = rindex;
  277. return 0;
  278. }
  279. static int ads1015_set_data_rate(struct ads1015_data *data, int chan, int rate)
  280. {
  281. int i, ret, rindex = -1;
  282. for (i = 0; i < ARRAY_SIZE(ads1015_data_rate); i++)
  283. if (data->data_rate[i] == rate) {
  284. rindex = i;
  285. break;
  286. }
  287. if (rindex < 0)
  288. return -EINVAL;
  289. ret = regmap_update_bits(data->regmap, ADS1015_CFG_REG,
  290. ADS1015_CFG_DR_MASK,
  291. rindex << ADS1015_CFG_DR_SHIFT);
  292. if (ret < 0)
  293. return ret;
  294. data->channel_data[chan].data_rate = rindex;
  295. return 0;
  296. }
  297. static int ads1015_read_raw(struct iio_dev *indio_dev,
  298. struct iio_chan_spec const *chan, int *val,
  299. int *val2, long mask)
  300. {
  301. int ret, idx;
  302. struct ads1015_data *data = iio_priv(indio_dev);
  303. mutex_lock(&indio_dev->mlock);
  304. mutex_lock(&data->lock);
  305. switch (mask) {
  306. case IIO_CHAN_INFO_RAW: {
  307. int shift = chan->scan_type.shift;
  308. if (iio_buffer_enabled(indio_dev)) {
  309. ret = -EBUSY;
  310. break;
  311. }
  312. ret = ads1015_set_power_state(data, true);
  313. if (ret < 0)
  314. break;
  315. ret = ads1015_get_adc_result(data, chan->address, val);
  316. if (ret < 0) {
  317. ads1015_set_power_state(data, false);
  318. break;
  319. }
  320. *val = sign_extend32(*val >> shift, 15 - shift);
  321. ret = ads1015_set_power_state(data, false);
  322. if (ret < 0)
  323. break;
  324. ret = IIO_VAL_INT;
  325. break;
  326. }
  327. case IIO_CHAN_INFO_SCALE:
  328. idx = data->channel_data[chan->address].pga;
  329. *val = ads1015_scale[idx].scale;
  330. *val2 = ads1015_scale[idx].uscale;
  331. ret = IIO_VAL_INT_PLUS_MICRO;
  332. break;
  333. case IIO_CHAN_INFO_SAMP_FREQ:
  334. idx = data->channel_data[chan->address].data_rate;
  335. *val = data->data_rate[idx];
  336. ret = IIO_VAL_INT;
  337. break;
  338. default:
  339. ret = -EINVAL;
  340. break;
  341. }
  342. mutex_unlock(&data->lock);
  343. mutex_unlock(&indio_dev->mlock);
  344. return ret;
  345. }
  346. static int ads1015_write_raw(struct iio_dev *indio_dev,
  347. struct iio_chan_spec const *chan, int val,
  348. int val2, long mask)
  349. {
  350. struct ads1015_data *data = iio_priv(indio_dev);
  351. int ret;
  352. mutex_lock(&data->lock);
  353. switch (mask) {
  354. case IIO_CHAN_INFO_SCALE:
  355. ret = ads1015_set_scale(data, chan->address, val, val2);
  356. break;
  357. case IIO_CHAN_INFO_SAMP_FREQ:
  358. ret = ads1015_set_data_rate(data, chan->address, val);
  359. break;
  360. default:
  361. ret = -EINVAL;
  362. break;
  363. }
  364. mutex_unlock(&data->lock);
  365. return ret;
  366. }
  367. static int ads1015_buffer_preenable(struct iio_dev *indio_dev)
  368. {
  369. return ads1015_set_power_state(iio_priv(indio_dev), true);
  370. }
  371. static int ads1015_buffer_postdisable(struct iio_dev *indio_dev)
  372. {
  373. return ads1015_set_power_state(iio_priv(indio_dev), false);
  374. }
  375. static const struct iio_buffer_setup_ops ads1015_buffer_setup_ops = {
  376. .preenable = ads1015_buffer_preenable,
  377. .postenable = iio_triggered_buffer_postenable,
  378. .predisable = iio_triggered_buffer_predisable,
  379. .postdisable = ads1015_buffer_postdisable,
  380. .validate_scan_mask = &iio_validate_scan_mask_onehot,
  381. };
  382. static IIO_CONST_ATTR(scale_available, "3 2 1 0.5 0.25 0.125");
  383. static IIO_CONST_ATTR_NAMED(ads1015_sampling_frequency_available,
  384. sampling_frequency_available, "128 250 490 920 1600 2400 3300");
  385. static IIO_CONST_ATTR_NAMED(ads1115_sampling_frequency_available,
  386. sampling_frequency_available, "8 16 32 64 128 250 475 860");
  387. static struct attribute *ads1015_attributes[] = {
  388. &iio_const_attr_scale_available.dev_attr.attr,
  389. &iio_const_attr_ads1015_sampling_frequency_available.dev_attr.attr,
  390. NULL,
  391. };
  392. static const struct attribute_group ads1015_attribute_group = {
  393. .attrs = ads1015_attributes,
  394. };
  395. static struct attribute *ads1115_attributes[] = {
  396. &iio_const_attr_scale_available.dev_attr.attr,
  397. &iio_const_attr_ads1115_sampling_frequency_available.dev_attr.attr,
  398. NULL,
  399. };
  400. static const struct attribute_group ads1115_attribute_group = {
  401. .attrs = ads1115_attributes,
  402. };
  403. static struct iio_info ads1015_info = {
  404. .driver_module = THIS_MODULE,
  405. .read_raw = ads1015_read_raw,
  406. .write_raw = ads1015_write_raw,
  407. .attrs = &ads1015_attribute_group,
  408. };
  409. static struct iio_info ads1115_info = {
  410. .driver_module = THIS_MODULE,
  411. .read_raw = ads1015_read_raw,
  412. .write_raw = ads1015_write_raw,
  413. .attrs = &ads1115_attribute_group,
  414. };
  415. #ifdef CONFIG_OF
  416. static int ads1015_get_channels_config_of(struct i2c_client *client)
  417. {
  418. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  419. struct ads1015_data *data = iio_priv(indio_dev);
  420. struct device_node *node;
  421. if (!client->dev.of_node ||
  422. !of_get_next_child(client->dev.of_node, NULL))
  423. return -EINVAL;
  424. for_each_child_of_node(client->dev.of_node, node) {
  425. u32 pval;
  426. unsigned int channel;
  427. unsigned int pga = ADS1015_DEFAULT_PGA;
  428. unsigned int data_rate = ADS1015_DEFAULT_DATA_RATE;
  429. if (of_property_read_u32(node, "reg", &pval)) {
  430. dev_err(&client->dev, "invalid reg on %s\n",
  431. node->full_name);
  432. continue;
  433. }
  434. channel = pval;
  435. if (channel >= ADS1015_CHANNELS) {
  436. dev_err(&client->dev,
  437. "invalid channel index %d on %s\n",
  438. channel, node->full_name);
  439. continue;
  440. }
  441. if (!of_property_read_u32(node, "ti,gain", &pval)) {
  442. pga = pval;
  443. if (pga > 6) {
  444. dev_err(&client->dev, "invalid gain on %s\n",
  445. node->full_name);
  446. of_node_put(node);
  447. return -EINVAL;
  448. }
  449. }
  450. if (!of_property_read_u32(node, "ti,datarate", &pval)) {
  451. data_rate = pval;
  452. if (data_rate > 7) {
  453. dev_err(&client->dev,
  454. "invalid data_rate on %s\n",
  455. node->full_name);
  456. of_node_put(node);
  457. return -EINVAL;
  458. }
  459. }
  460. data->channel_data[channel].pga = pga;
  461. data->channel_data[channel].data_rate = data_rate;
  462. }
  463. return 0;
  464. }
  465. #endif
  466. static void ads1015_get_channels_config(struct i2c_client *client)
  467. {
  468. unsigned int k;
  469. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  470. struct ads1015_data *data = iio_priv(indio_dev);
  471. struct ads1015_platform_data *pdata = dev_get_platdata(&client->dev);
  472. /* prefer platform data */
  473. if (pdata) {
  474. memcpy(data->channel_data, pdata->channel_data,
  475. sizeof(data->channel_data));
  476. return;
  477. }
  478. #ifdef CONFIG_OF
  479. if (!ads1015_get_channels_config_of(client))
  480. return;
  481. #endif
  482. /* fallback on default configuration */
  483. for (k = 0; k < ADS1015_CHANNELS; ++k) {
  484. data->channel_data[k].pga = ADS1015_DEFAULT_PGA;
  485. data->channel_data[k].data_rate = ADS1015_DEFAULT_DATA_RATE;
  486. }
  487. }
  488. static int ads1015_probe(struct i2c_client *client,
  489. const struct i2c_device_id *id)
  490. {
  491. struct iio_dev *indio_dev;
  492. struct ads1015_data *data;
  493. int ret;
  494. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  495. if (!indio_dev)
  496. return -ENOMEM;
  497. data = iio_priv(indio_dev);
  498. i2c_set_clientdata(client, indio_dev);
  499. mutex_init(&data->lock);
  500. indio_dev->dev.parent = &client->dev;
  501. indio_dev->dev.of_node = client->dev.of_node;
  502. indio_dev->name = ADS1015_DRV_NAME;
  503. indio_dev->modes = INDIO_DIRECT_MODE;
  504. switch (id->driver_data) {
  505. case ADS1015:
  506. indio_dev->channels = ads1015_channels;
  507. indio_dev->num_channels = ARRAY_SIZE(ads1015_channels);
  508. indio_dev->info = &ads1015_info;
  509. data->data_rate = (unsigned int *) &ads1015_data_rate;
  510. break;
  511. case ADS1115:
  512. indio_dev->channels = ads1115_channels;
  513. indio_dev->num_channels = ARRAY_SIZE(ads1115_channels);
  514. indio_dev->info = &ads1115_info;
  515. data->data_rate = (unsigned int *) &ads1115_data_rate;
  516. break;
  517. }
  518. /* we need to keep this ABI the same as used by hwmon ADS1015 driver */
  519. ads1015_get_channels_config(client);
  520. data->regmap = devm_regmap_init_i2c(client, &ads1015_regmap_config);
  521. if (IS_ERR(data->regmap)) {
  522. dev_err(&client->dev, "Failed to allocate register map\n");
  523. return PTR_ERR(data->regmap);
  524. }
  525. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  526. ads1015_trigger_handler,
  527. &ads1015_buffer_setup_ops);
  528. if (ret < 0) {
  529. dev_err(&client->dev, "iio triggered buffer setup failed\n");
  530. return ret;
  531. }
  532. ret = pm_runtime_set_active(&client->dev);
  533. if (ret)
  534. goto err_buffer_cleanup;
  535. pm_runtime_set_autosuspend_delay(&client->dev, ADS1015_SLEEP_DELAY_MS);
  536. pm_runtime_use_autosuspend(&client->dev);
  537. pm_runtime_enable(&client->dev);
  538. ret = iio_device_register(indio_dev);
  539. if (ret < 0) {
  540. dev_err(&client->dev, "Failed to register IIO device\n");
  541. goto err_buffer_cleanup;
  542. }
  543. return 0;
  544. err_buffer_cleanup:
  545. iio_triggered_buffer_cleanup(indio_dev);
  546. return ret;
  547. }
  548. static int ads1015_remove(struct i2c_client *client)
  549. {
  550. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  551. struct ads1015_data *data = iio_priv(indio_dev);
  552. iio_device_unregister(indio_dev);
  553. pm_runtime_disable(&client->dev);
  554. pm_runtime_set_suspended(&client->dev);
  555. pm_runtime_put_noidle(&client->dev);
  556. iio_triggered_buffer_cleanup(indio_dev);
  557. /* power down single shot mode */
  558. return regmap_update_bits(data->regmap, ADS1015_CFG_REG,
  559. ADS1015_CFG_MOD_MASK,
  560. ADS1015_SINGLESHOT << ADS1015_CFG_MOD_SHIFT);
  561. }
  562. #ifdef CONFIG_PM
  563. static int ads1015_runtime_suspend(struct device *dev)
  564. {
  565. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  566. struct ads1015_data *data = iio_priv(indio_dev);
  567. return regmap_update_bits(data->regmap, ADS1015_CFG_REG,
  568. ADS1015_CFG_MOD_MASK,
  569. ADS1015_SINGLESHOT << ADS1015_CFG_MOD_SHIFT);
  570. }
  571. static int ads1015_runtime_resume(struct device *dev)
  572. {
  573. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  574. struct ads1015_data *data = iio_priv(indio_dev);
  575. return regmap_update_bits(data->regmap, ADS1015_CFG_REG,
  576. ADS1015_CFG_MOD_MASK,
  577. ADS1015_CONTINUOUS << ADS1015_CFG_MOD_SHIFT);
  578. }
  579. #endif
  580. static const struct dev_pm_ops ads1015_pm_ops = {
  581. SET_RUNTIME_PM_OPS(ads1015_runtime_suspend,
  582. ads1015_runtime_resume, NULL)
  583. };
  584. static const struct i2c_device_id ads1015_id[] = {
  585. {"ads1015", ADS1015},
  586. {"ads1115", ADS1115},
  587. {}
  588. };
  589. MODULE_DEVICE_TABLE(i2c, ads1015_id);
  590. static struct i2c_driver ads1015_driver = {
  591. .driver = {
  592. .name = ADS1015_DRV_NAME,
  593. .pm = &ads1015_pm_ops,
  594. },
  595. .probe = ads1015_probe,
  596. .remove = ads1015_remove,
  597. .id_table = ads1015_id,
  598. };
  599. module_i2c_driver(ads1015_driver);
  600. MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
  601. MODULE_DESCRIPTION("Texas Instruments ADS1015 ADC driver");
  602. MODULE_LICENSE("GPL v2");