rpr0521.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. /*
  2. * RPR-0521 ROHM Ambient Light and Proximity Sensor
  3. *
  4. * Copyright (c) 2015, 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 RPR-0521RS (7-bit I2C slave address 0x38).
  11. *
  12. * TODO: illuminance channel
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/i2c.h>
  17. #include <linux/regmap.h>
  18. #include <linux/delay.h>
  19. #include <linux/acpi.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/buffer.h>
  22. #include <linux/iio/trigger.h>
  23. #include <linux/iio/trigger_consumer.h>
  24. #include <linux/iio/triggered_buffer.h>
  25. #include <linux/iio/sysfs.h>
  26. #include <linux/pm_runtime.h>
  27. #define RPR0521_REG_SYSTEM_CTRL 0x40
  28. #define RPR0521_REG_MODE_CTRL 0x41
  29. #define RPR0521_REG_ALS_CTRL 0x42
  30. #define RPR0521_REG_PXS_CTRL 0x43
  31. #define RPR0521_REG_PXS_DATA 0x44 /* 16-bit, little endian */
  32. #define RPR0521_REG_ALS_DATA0 0x46 /* 16-bit, little endian */
  33. #define RPR0521_REG_ALS_DATA1 0x48 /* 16-bit, little endian */
  34. #define RPR0521_REG_INTERRUPT 0x4A
  35. #define RPR0521_REG_PS_OFFSET_LSB 0x53
  36. #define RPR0521_REG_ID 0x92
  37. #define RPR0521_MODE_ALS_MASK BIT(7)
  38. #define RPR0521_MODE_PXS_MASK BIT(6)
  39. #define RPR0521_MODE_MEAS_TIME_MASK GENMASK(3, 0)
  40. #define RPR0521_ALS_DATA0_GAIN_MASK GENMASK(5, 4)
  41. #define RPR0521_ALS_DATA0_GAIN_SHIFT 4
  42. #define RPR0521_ALS_DATA1_GAIN_MASK GENMASK(3, 2)
  43. #define RPR0521_ALS_DATA1_GAIN_SHIFT 2
  44. #define RPR0521_PXS_GAIN_MASK GENMASK(5, 4)
  45. #define RPR0521_PXS_GAIN_SHIFT 4
  46. #define RPR0521_PXS_PERSISTENCE_MASK GENMASK(3, 0)
  47. #define RPR0521_INTERRUPT_INT_TRIG_PS_MASK BIT(0)
  48. #define RPR0521_INTERRUPT_INT_TRIG_ALS_MASK BIT(1)
  49. #define RPR0521_INTERRUPT_INT_REASSERT_MASK BIT(3)
  50. #define RPR0521_INTERRUPT_ALS_INT_STATUS_MASK BIT(6)
  51. #define RPR0521_INTERRUPT_PS_INT_STATUS_MASK BIT(7)
  52. #define RPR0521_MODE_ALS_ENABLE BIT(7)
  53. #define RPR0521_MODE_ALS_DISABLE 0x00
  54. #define RPR0521_MODE_PXS_ENABLE BIT(6)
  55. #define RPR0521_MODE_PXS_DISABLE 0x00
  56. #define RPR0521_PXS_PERSISTENCE_DRDY 0x00
  57. #define RPR0521_INTERRUPT_INT_TRIG_PS_ENABLE BIT(0)
  58. #define RPR0521_INTERRUPT_INT_TRIG_PS_DISABLE 0x00
  59. #define RPR0521_INTERRUPT_INT_TRIG_ALS_ENABLE BIT(1)
  60. #define RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE 0x00
  61. #define RPR0521_INTERRUPT_INT_REASSERT_ENABLE BIT(3)
  62. #define RPR0521_INTERRUPT_INT_REASSERT_DISABLE 0x00
  63. #define RPR0521_MANUFACT_ID 0xE0
  64. #define RPR0521_DEFAULT_MEAS_TIME 0x06 /* ALS - 100ms, PXS - 100ms */
  65. #define RPR0521_DRV_NAME "RPR0521"
  66. #define RPR0521_IRQ_NAME "rpr0521_event"
  67. #define RPR0521_REGMAP_NAME "rpr0521_regmap"
  68. #define RPR0521_SLEEP_DELAY_MS 2000
  69. #define RPR0521_ALS_SCALE_AVAIL "0.007812 0.015625 0.5 1"
  70. #define RPR0521_PXS_SCALE_AVAIL "0.125 0.5 1"
  71. struct rpr0521_gain {
  72. int scale;
  73. int uscale;
  74. };
  75. static const struct rpr0521_gain rpr0521_als_gain[4] = {
  76. {1, 0}, /* x1 */
  77. {0, 500000}, /* x2 */
  78. {0, 15625}, /* x64 */
  79. {0, 7812}, /* x128 */
  80. };
  81. static const struct rpr0521_gain rpr0521_pxs_gain[3] = {
  82. {1, 0}, /* x1 */
  83. {0, 500000}, /* x2 */
  84. {0, 125000}, /* x4 */
  85. };
  86. enum rpr0521_channel {
  87. RPR0521_CHAN_PXS,
  88. RPR0521_CHAN_ALS_DATA0,
  89. RPR0521_CHAN_ALS_DATA1,
  90. };
  91. struct rpr0521_reg_desc {
  92. u8 address;
  93. u8 device_mask;
  94. };
  95. static const struct rpr0521_reg_desc rpr0521_data_reg[] = {
  96. [RPR0521_CHAN_PXS] = {
  97. .address = RPR0521_REG_PXS_DATA,
  98. .device_mask = RPR0521_MODE_PXS_MASK,
  99. },
  100. [RPR0521_CHAN_ALS_DATA0] = {
  101. .address = RPR0521_REG_ALS_DATA0,
  102. .device_mask = RPR0521_MODE_ALS_MASK,
  103. },
  104. [RPR0521_CHAN_ALS_DATA1] = {
  105. .address = RPR0521_REG_ALS_DATA1,
  106. .device_mask = RPR0521_MODE_ALS_MASK,
  107. },
  108. };
  109. static const struct rpr0521_gain_info {
  110. u8 reg;
  111. u8 mask;
  112. u8 shift;
  113. const struct rpr0521_gain *gain;
  114. int size;
  115. } rpr0521_gain[] = {
  116. [RPR0521_CHAN_PXS] = {
  117. .reg = RPR0521_REG_PXS_CTRL,
  118. .mask = RPR0521_PXS_GAIN_MASK,
  119. .shift = RPR0521_PXS_GAIN_SHIFT,
  120. .gain = rpr0521_pxs_gain,
  121. .size = ARRAY_SIZE(rpr0521_pxs_gain),
  122. },
  123. [RPR0521_CHAN_ALS_DATA0] = {
  124. .reg = RPR0521_REG_ALS_CTRL,
  125. .mask = RPR0521_ALS_DATA0_GAIN_MASK,
  126. .shift = RPR0521_ALS_DATA0_GAIN_SHIFT,
  127. .gain = rpr0521_als_gain,
  128. .size = ARRAY_SIZE(rpr0521_als_gain),
  129. },
  130. [RPR0521_CHAN_ALS_DATA1] = {
  131. .reg = RPR0521_REG_ALS_CTRL,
  132. .mask = RPR0521_ALS_DATA1_GAIN_MASK,
  133. .shift = RPR0521_ALS_DATA1_GAIN_SHIFT,
  134. .gain = rpr0521_als_gain,
  135. .size = ARRAY_SIZE(rpr0521_als_gain),
  136. },
  137. };
  138. struct rpr0521_samp_freq {
  139. int als_hz;
  140. int als_uhz;
  141. int pxs_hz;
  142. int pxs_uhz;
  143. };
  144. static const struct rpr0521_samp_freq rpr0521_samp_freq_i[13] = {
  145. /* {ALS, PXS}, W==currently writable option */
  146. {0, 0, 0, 0}, /* W0000, 0=standby */
  147. {0, 0, 100, 0}, /* 0001 */
  148. {0, 0, 25, 0}, /* 0010 */
  149. {0, 0, 10, 0}, /* 0011 */
  150. {0, 0, 2, 500000}, /* 0100 */
  151. {10, 0, 20, 0}, /* 0101 */
  152. {10, 0, 10, 0}, /* W0110 */
  153. {10, 0, 2, 500000}, /* 0111 */
  154. {2, 500000, 20, 0}, /* 1000, measurement 100ms, sleep 300ms */
  155. {2, 500000, 10, 0}, /* 1001, measurement 100ms, sleep 300ms */
  156. {2, 500000, 0, 0}, /* 1010, high sensitivity mode */
  157. {2, 500000, 2, 500000}, /* W1011, high sensitivity mode */
  158. {20, 0, 20, 0} /* 1100, ALS_data x 0.5, see specification P.18 */
  159. };
  160. struct rpr0521_data {
  161. struct i2c_client *client;
  162. /* protect device params updates (e.g state, gain) */
  163. struct mutex lock;
  164. /* device active status */
  165. bool als_dev_en;
  166. bool pxs_dev_en;
  167. struct iio_trigger *drdy_trigger0;
  168. s64 irq_timestamp;
  169. /* optimize runtime pm ops - enable/disable device only if needed */
  170. bool als_ps_need_en;
  171. bool pxs_ps_need_en;
  172. bool als_need_dis;
  173. bool pxs_need_dis;
  174. struct regmap *regmap;
  175. };
  176. static IIO_CONST_ATTR(in_intensity_scale_available, RPR0521_ALS_SCALE_AVAIL);
  177. static IIO_CONST_ATTR(in_proximity_scale_available, RPR0521_PXS_SCALE_AVAIL);
  178. /*
  179. * Start with easy freq first, whole table of freq combinations is more
  180. * complicated.
  181. */
  182. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("2.5 10");
  183. static struct attribute *rpr0521_attributes[] = {
  184. &iio_const_attr_in_intensity_scale_available.dev_attr.attr,
  185. &iio_const_attr_in_proximity_scale_available.dev_attr.attr,
  186. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  187. NULL,
  188. };
  189. static const struct attribute_group rpr0521_attribute_group = {
  190. .attrs = rpr0521_attributes,
  191. };
  192. /* Order of the channel data in buffer */
  193. enum rpr0521_scan_index_order {
  194. RPR0521_CHAN_INDEX_PXS,
  195. RPR0521_CHAN_INDEX_BOTH,
  196. RPR0521_CHAN_INDEX_IR,
  197. };
  198. static const unsigned long rpr0521_available_scan_masks[] = {
  199. BIT(RPR0521_CHAN_INDEX_PXS) | BIT(RPR0521_CHAN_INDEX_BOTH) |
  200. BIT(RPR0521_CHAN_INDEX_IR),
  201. 0
  202. };
  203. static const struct iio_chan_spec rpr0521_channels[] = {
  204. {
  205. .type = IIO_PROXIMITY,
  206. .address = RPR0521_CHAN_PXS,
  207. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  208. BIT(IIO_CHAN_INFO_OFFSET) |
  209. BIT(IIO_CHAN_INFO_SCALE),
  210. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  211. .scan_index = RPR0521_CHAN_INDEX_PXS,
  212. .scan_type = {
  213. .sign = 'u',
  214. .realbits = 16,
  215. .storagebits = 16,
  216. .endianness = IIO_LE,
  217. },
  218. },
  219. {
  220. .type = IIO_INTENSITY,
  221. .modified = 1,
  222. .address = RPR0521_CHAN_ALS_DATA0,
  223. .channel2 = IIO_MOD_LIGHT_BOTH,
  224. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  225. BIT(IIO_CHAN_INFO_SCALE),
  226. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  227. .scan_index = RPR0521_CHAN_INDEX_BOTH,
  228. .scan_type = {
  229. .sign = 'u',
  230. .realbits = 16,
  231. .storagebits = 16,
  232. .endianness = IIO_LE,
  233. },
  234. },
  235. {
  236. .type = IIO_INTENSITY,
  237. .modified = 1,
  238. .address = RPR0521_CHAN_ALS_DATA1,
  239. .channel2 = IIO_MOD_LIGHT_IR,
  240. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  241. BIT(IIO_CHAN_INFO_SCALE),
  242. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  243. .scan_index = RPR0521_CHAN_INDEX_IR,
  244. .scan_type = {
  245. .sign = 'u',
  246. .realbits = 16,
  247. .storagebits = 16,
  248. .endianness = IIO_LE,
  249. },
  250. },
  251. };
  252. static int rpr0521_als_enable(struct rpr0521_data *data, u8 status)
  253. {
  254. int ret;
  255. ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL,
  256. RPR0521_MODE_ALS_MASK,
  257. status);
  258. if (ret < 0)
  259. return ret;
  260. if (status & RPR0521_MODE_ALS_MASK)
  261. data->als_dev_en = true;
  262. else
  263. data->als_dev_en = false;
  264. return 0;
  265. }
  266. static int rpr0521_pxs_enable(struct rpr0521_data *data, u8 status)
  267. {
  268. int ret;
  269. ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL,
  270. RPR0521_MODE_PXS_MASK,
  271. status);
  272. if (ret < 0)
  273. return ret;
  274. if (status & RPR0521_MODE_PXS_MASK)
  275. data->pxs_dev_en = true;
  276. else
  277. data->pxs_dev_en = false;
  278. return 0;
  279. }
  280. /**
  281. * rpr0521_set_power_state - handles runtime PM state and sensors enabled status
  282. *
  283. * @data: rpr0521 device private data
  284. * @on: state to be set for devices in @device_mask
  285. * @device_mask: bitmask specifying for which device we need to update @on state
  286. *
  287. * Calls for this function must be balanced so that each ON should have matching
  288. * OFF. Otherwise pm usage_count gets out of sync.
  289. */
  290. static int rpr0521_set_power_state(struct rpr0521_data *data, bool on,
  291. u8 device_mask)
  292. {
  293. #ifdef CONFIG_PM
  294. int ret;
  295. if (device_mask & RPR0521_MODE_ALS_MASK) {
  296. data->als_ps_need_en = on;
  297. data->als_need_dis = !on;
  298. }
  299. if (device_mask & RPR0521_MODE_PXS_MASK) {
  300. data->pxs_ps_need_en = on;
  301. data->pxs_need_dis = !on;
  302. }
  303. /*
  304. * On: _resume() is called only when we are suspended
  305. * Off: _suspend() is called after delay if _resume() is not
  306. * called before that.
  307. * Note: If either measurement is re-enabled before _suspend(),
  308. * both stay enabled until _suspend().
  309. */
  310. if (on) {
  311. ret = pm_runtime_get_sync(&data->client->dev);
  312. } else {
  313. pm_runtime_mark_last_busy(&data->client->dev);
  314. ret = pm_runtime_put_autosuspend(&data->client->dev);
  315. }
  316. if (ret < 0) {
  317. dev_err(&data->client->dev,
  318. "Failed: rpr0521_set_power_state for %d, ret %d\n",
  319. on, ret);
  320. if (on)
  321. pm_runtime_put_noidle(&data->client->dev);
  322. return ret;
  323. }
  324. if (on) {
  325. /* If _resume() was not called, enable measurement now. */
  326. if (data->als_ps_need_en) {
  327. ret = rpr0521_als_enable(data, RPR0521_MODE_ALS_ENABLE);
  328. if (ret)
  329. return ret;
  330. data->als_ps_need_en = false;
  331. }
  332. if (data->pxs_ps_need_en) {
  333. ret = rpr0521_pxs_enable(data, RPR0521_MODE_PXS_ENABLE);
  334. if (ret)
  335. return ret;
  336. data->pxs_ps_need_en = false;
  337. }
  338. }
  339. #endif
  340. return 0;
  341. }
  342. /* Interrupt register tells if this sensor caused the interrupt or not. */
  343. static inline bool rpr0521_is_triggered(struct rpr0521_data *data)
  344. {
  345. int ret;
  346. int reg;
  347. ret = regmap_read(data->regmap, RPR0521_REG_INTERRUPT, &reg);
  348. if (ret < 0)
  349. return false; /* Reg read failed. */
  350. if (reg &
  351. (RPR0521_INTERRUPT_ALS_INT_STATUS_MASK |
  352. RPR0521_INTERRUPT_PS_INT_STATUS_MASK))
  353. return true;
  354. else
  355. return false; /* Int not from this sensor. */
  356. }
  357. /* IRQ to trigger handler */
  358. static irqreturn_t rpr0521_drdy_irq_handler(int irq, void *private)
  359. {
  360. struct iio_dev *indio_dev = private;
  361. struct rpr0521_data *data = iio_priv(indio_dev);
  362. data->irq_timestamp = iio_get_time_ns(indio_dev);
  363. /*
  364. * We need to wake the thread to read the interrupt reg. It
  365. * is not possible to do that here because regmap_read takes a
  366. * mutex.
  367. */
  368. return IRQ_WAKE_THREAD;
  369. }
  370. static irqreturn_t rpr0521_drdy_irq_thread(int irq, void *private)
  371. {
  372. struct iio_dev *indio_dev = private;
  373. struct rpr0521_data *data = iio_priv(indio_dev);
  374. if (rpr0521_is_triggered(data)) {
  375. iio_trigger_poll_chained(data->drdy_trigger0);
  376. return IRQ_HANDLED;
  377. }
  378. return IRQ_NONE;
  379. }
  380. static irqreturn_t rpr0521_trigger_consumer_store_time(int irq, void *p)
  381. {
  382. struct iio_poll_func *pf = p;
  383. struct iio_dev *indio_dev = pf->indio_dev;
  384. /* Other trigger polls store time here. */
  385. if (!iio_trigger_using_own(indio_dev))
  386. pf->timestamp = iio_get_time_ns(indio_dev);
  387. return IRQ_WAKE_THREAD;
  388. }
  389. static irqreturn_t rpr0521_trigger_consumer_handler(int irq, void *p)
  390. {
  391. struct iio_poll_func *pf = p;
  392. struct iio_dev *indio_dev = pf->indio_dev;
  393. struct rpr0521_data *data = iio_priv(indio_dev);
  394. int err;
  395. u8 buffer[16]; /* 3 16-bit channels + padding + ts */
  396. /* Use irq timestamp when reasonable. */
  397. if (iio_trigger_using_own(indio_dev) && data->irq_timestamp) {
  398. pf->timestamp = data->irq_timestamp;
  399. data->irq_timestamp = 0;
  400. }
  401. /* Other chained trigger polls get timestamp only here. */
  402. if (!pf->timestamp)
  403. pf->timestamp = iio_get_time_ns(indio_dev);
  404. err = regmap_bulk_read(data->regmap, RPR0521_REG_PXS_DATA,
  405. &buffer,
  406. (3 * 2) + 1); /* 3 * 16-bit + (discarded) int clear reg. */
  407. if (!err)
  408. iio_push_to_buffers_with_timestamp(indio_dev,
  409. buffer, pf->timestamp);
  410. else
  411. dev_err(&data->client->dev,
  412. "Trigger consumer can't read from sensor.\n");
  413. pf->timestamp = 0;
  414. iio_trigger_notify_done(indio_dev->trig);
  415. return IRQ_HANDLED;
  416. }
  417. static int rpr0521_write_int_enable(struct rpr0521_data *data)
  418. {
  419. int err;
  420. /* Interrupt after each measurement */
  421. err = regmap_update_bits(data->regmap, RPR0521_REG_PXS_CTRL,
  422. RPR0521_PXS_PERSISTENCE_MASK,
  423. RPR0521_PXS_PERSISTENCE_DRDY);
  424. if (err) {
  425. dev_err(&data->client->dev, "PS control reg write fail.\n");
  426. return -EBUSY;
  427. }
  428. /* Ignore latch and mode because of drdy */
  429. err = regmap_write(data->regmap, RPR0521_REG_INTERRUPT,
  430. RPR0521_INTERRUPT_INT_REASSERT_DISABLE |
  431. RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE |
  432. RPR0521_INTERRUPT_INT_TRIG_PS_ENABLE
  433. );
  434. if (err) {
  435. dev_err(&data->client->dev, "Interrupt setup write fail.\n");
  436. return -EBUSY;
  437. }
  438. return 0;
  439. }
  440. static int rpr0521_write_int_disable(struct rpr0521_data *data)
  441. {
  442. /* Don't care of clearing mode, assert and latch. */
  443. return regmap_write(data->regmap, RPR0521_REG_INTERRUPT,
  444. RPR0521_INTERRUPT_INT_TRIG_ALS_DISABLE |
  445. RPR0521_INTERRUPT_INT_TRIG_PS_DISABLE
  446. );
  447. }
  448. /*
  449. * Trigger producer enable / disable. Note that there will be trigs only when
  450. * measurement data is ready to be read.
  451. */
  452. static int rpr0521_pxs_drdy_set_state(struct iio_trigger *trigger,
  453. bool enable_drdy)
  454. {
  455. struct iio_dev *indio_dev = iio_trigger_get_drvdata(trigger);
  456. struct rpr0521_data *data = iio_priv(indio_dev);
  457. int err;
  458. if (enable_drdy)
  459. err = rpr0521_write_int_enable(data);
  460. else
  461. err = rpr0521_write_int_disable(data);
  462. if (err)
  463. dev_err(&data->client->dev, "rpr0521_pxs_drdy_set_state failed\n");
  464. return err;
  465. }
  466. static const struct iio_trigger_ops rpr0521_trigger_ops = {
  467. .set_trigger_state = rpr0521_pxs_drdy_set_state,
  468. };
  469. static int rpr0521_buffer_preenable(struct iio_dev *indio_dev)
  470. {
  471. int err;
  472. struct rpr0521_data *data = iio_priv(indio_dev);
  473. mutex_lock(&data->lock);
  474. err = rpr0521_set_power_state(data, true,
  475. (RPR0521_MODE_PXS_MASK | RPR0521_MODE_ALS_MASK));
  476. mutex_unlock(&data->lock);
  477. if (err)
  478. dev_err(&data->client->dev, "_buffer_preenable fail\n");
  479. return err;
  480. }
  481. static int rpr0521_buffer_postdisable(struct iio_dev *indio_dev)
  482. {
  483. int err;
  484. struct rpr0521_data *data = iio_priv(indio_dev);
  485. mutex_lock(&data->lock);
  486. err = rpr0521_set_power_state(data, false,
  487. (RPR0521_MODE_PXS_MASK | RPR0521_MODE_ALS_MASK));
  488. mutex_unlock(&data->lock);
  489. if (err)
  490. dev_err(&data->client->dev, "_buffer_postdisable fail\n");
  491. return err;
  492. }
  493. static const struct iio_buffer_setup_ops rpr0521_buffer_setup_ops = {
  494. .preenable = rpr0521_buffer_preenable,
  495. .postenable = iio_triggered_buffer_postenable,
  496. .predisable = iio_triggered_buffer_predisable,
  497. .postdisable = rpr0521_buffer_postdisable,
  498. };
  499. static int rpr0521_get_gain(struct rpr0521_data *data, int chan,
  500. int *val, int *val2)
  501. {
  502. int ret, reg, idx;
  503. ret = regmap_read(data->regmap, rpr0521_gain[chan].reg, &reg);
  504. if (ret < 0)
  505. return ret;
  506. idx = (rpr0521_gain[chan].mask & reg) >> rpr0521_gain[chan].shift;
  507. *val = rpr0521_gain[chan].gain[idx].scale;
  508. *val2 = rpr0521_gain[chan].gain[idx].uscale;
  509. return 0;
  510. }
  511. static int rpr0521_set_gain(struct rpr0521_data *data, int chan,
  512. int val, int val2)
  513. {
  514. int i, idx = -EINVAL;
  515. /* get gain index */
  516. for (i = 0; i < rpr0521_gain[chan].size; i++)
  517. if (val == rpr0521_gain[chan].gain[i].scale &&
  518. val2 == rpr0521_gain[chan].gain[i].uscale) {
  519. idx = i;
  520. break;
  521. }
  522. if (idx < 0)
  523. return idx;
  524. return regmap_update_bits(data->regmap, rpr0521_gain[chan].reg,
  525. rpr0521_gain[chan].mask,
  526. idx << rpr0521_gain[chan].shift);
  527. }
  528. static int rpr0521_read_samp_freq(struct rpr0521_data *data,
  529. enum iio_chan_type chan_type,
  530. int *val, int *val2)
  531. {
  532. int reg, ret;
  533. ret = regmap_read(data->regmap, RPR0521_REG_MODE_CTRL, &reg);
  534. if (ret < 0)
  535. return ret;
  536. reg &= RPR0521_MODE_MEAS_TIME_MASK;
  537. if (reg >= ARRAY_SIZE(rpr0521_samp_freq_i))
  538. return -EINVAL;
  539. switch (chan_type) {
  540. case IIO_INTENSITY:
  541. *val = rpr0521_samp_freq_i[reg].als_hz;
  542. *val2 = rpr0521_samp_freq_i[reg].als_uhz;
  543. return 0;
  544. case IIO_PROXIMITY:
  545. *val = rpr0521_samp_freq_i[reg].pxs_hz;
  546. *val2 = rpr0521_samp_freq_i[reg].pxs_uhz;
  547. return 0;
  548. default:
  549. return -EINVAL;
  550. }
  551. }
  552. static int rpr0521_write_samp_freq_common(struct rpr0521_data *data,
  553. enum iio_chan_type chan_type,
  554. int val, int val2)
  555. {
  556. int i;
  557. /*
  558. * Ignore channel
  559. * both pxs and als are setup only to same freq because of simplicity
  560. */
  561. switch (val) {
  562. case 0:
  563. i = 0;
  564. break;
  565. case 2:
  566. if (val2 != 500000)
  567. return -EINVAL;
  568. i = 11;
  569. break;
  570. case 10:
  571. i = 6;
  572. break;
  573. default:
  574. return -EINVAL;
  575. }
  576. return regmap_update_bits(data->regmap,
  577. RPR0521_REG_MODE_CTRL,
  578. RPR0521_MODE_MEAS_TIME_MASK,
  579. i);
  580. }
  581. static int rpr0521_read_ps_offset(struct rpr0521_data *data, int *offset)
  582. {
  583. int ret;
  584. __le16 buffer;
  585. ret = regmap_bulk_read(data->regmap,
  586. RPR0521_REG_PS_OFFSET_LSB, &buffer, sizeof(buffer));
  587. if (ret < 0) {
  588. dev_err(&data->client->dev, "Failed to read PS OFFSET register\n");
  589. return ret;
  590. }
  591. *offset = le16_to_cpu(buffer);
  592. return ret;
  593. }
  594. static int rpr0521_write_ps_offset(struct rpr0521_data *data, int offset)
  595. {
  596. int ret;
  597. __le16 buffer;
  598. buffer = cpu_to_le16(offset & 0x3ff);
  599. ret = regmap_raw_write(data->regmap,
  600. RPR0521_REG_PS_OFFSET_LSB, &buffer, sizeof(buffer));
  601. if (ret < 0) {
  602. dev_err(&data->client->dev, "Failed to write PS OFFSET register\n");
  603. return ret;
  604. }
  605. return ret;
  606. }
  607. static int rpr0521_read_raw(struct iio_dev *indio_dev,
  608. struct iio_chan_spec const *chan, int *val,
  609. int *val2, long mask)
  610. {
  611. struct rpr0521_data *data = iio_priv(indio_dev);
  612. int ret;
  613. int busy;
  614. u8 device_mask;
  615. __le16 raw_data;
  616. switch (mask) {
  617. case IIO_CHAN_INFO_RAW:
  618. if (chan->type != IIO_INTENSITY && chan->type != IIO_PROXIMITY)
  619. return -EINVAL;
  620. busy = iio_device_claim_direct_mode(indio_dev);
  621. if (busy)
  622. return -EBUSY;
  623. device_mask = rpr0521_data_reg[chan->address].device_mask;
  624. mutex_lock(&data->lock);
  625. ret = rpr0521_set_power_state(data, true, device_mask);
  626. if (ret < 0)
  627. goto rpr0521_read_raw_out;
  628. ret = regmap_bulk_read(data->regmap,
  629. rpr0521_data_reg[chan->address].address,
  630. &raw_data, sizeof(raw_data));
  631. if (ret < 0) {
  632. rpr0521_set_power_state(data, false, device_mask);
  633. goto rpr0521_read_raw_out;
  634. }
  635. ret = rpr0521_set_power_state(data, false, device_mask);
  636. rpr0521_read_raw_out:
  637. mutex_unlock(&data->lock);
  638. iio_device_release_direct_mode(indio_dev);
  639. if (ret < 0)
  640. return ret;
  641. *val = le16_to_cpu(raw_data);
  642. return IIO_VAL_INT;
  643. case IIO_CHAN_INFO_SCALE:
  644. mutex_lock(&data->lock);
  645. ret = rpr0521_get_gain(data, chan->address, val, val2);
  646. mutex_unlock(&data->lock);
  647. if (ret < 0)
  648. return ret;
  649. return IIO_VAL_INT_PLUS_MICRO;
  650. case IIO_CHAN_INFO_SAMP_FREQ:
  651. mutex_lock(&data->lock);
  652. ret = rpr0521_read_samp_freq(data, chan->type, val, val2);
  653. mutex_unlock(&data->lock);
  654. if (ret < 0)
  655. return ret;
  656. return IIO_VAL_INT_PLUS_MICRO;
  657. case IIO_CHAN_INFO_OFFSET:
  658. mutex_lock(&data->lock);
  659. ret = rpr0521_read_ps_offset(data, val);
  660. mutex_unlock(&data->lock);
  661. if (ret < 0)
  662. return ret;
  663. return IIO_VAL_INT;
  664. default:
  665. return -EINVAL;
  666. }
  667. }
  668. static int rpr0521_write_raw(struct iio_dev *indio_dev,
  669. struct iio_chan_spec const *chan, int val,
  670. int val2, long mask)
  671. {
  672. struct rpr0521_data *data = iio_priv(indio_dev);
  673. int ret;
  674. switch (mask) {
  675. case IIO_CHAN_INFO_SCALE:
  676. mutex_lock(&data->lock);
  677. ret = rpr0521_set_gain(data, chan->address, val, val2);
  678. mutex_unlock(&data->lock);
  679. return ret;
  680. case IIO_CHAN_INFO_SAMP_FREQ:
  681. mutex_lock(&data->lock);
  682. ret = rpr0521_write_samp_freq_common(data, chan->type,
  683. val, val2);
  684. mutex_unlock(&data->lock);
  685. return ret;
  686. case IIO_CHAN_INFO_OFFSET:
  687. mutex_lock(&data->lock);
  688. ret = rpr0521_write_ps_offset(data, val);
  689. mutex_unlock(&data->lock);
  690. return ret;
  691. default:
  692. return -EINVAL;
  693. }
  694. }
  695. static const struct iio_info rpr0521_info = {
  696. .read_raw = rpr0521_read_raw,
  697. .write_raw = rpr0521_write_raw,
  698. .attrs = &rpr0521_attribute_group,
  699. };
  700. static int rpr0521_init(struct rpr0521_data *data)
  701. {
  702. int ret;
  703. int id;
  704. ret = regmap_read(data->regmap, RPR0521_REG_ID, &id);
  705. if (ret < 0) {
  706. dev_err(&data->client->dev, "Failed to read REG_ID register\n");
  707. return ret;
  708. }
  709. if (id != RPR0521_MANUFACT_ID) {
  710. dev_err(&data->client->dev, "Wrong id, got %x, expected %x\n",
  711. id, RPR0521_MANUFACT_ID);
  712. return -ENODEV;
  713. }
  714. /* set default measurement time - 100 ms for both ALS and PS */
  715. ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL,
  716. RPR0521_MODE_MEAS_TIME_MASK,
  717. RPR0521_DEFAULT_MEAS_TIME);
  718. if (ret) {
  719. pr_err("regmap_update_bits returned %d\n", ret);
  720. return ret;
  721. }
  722. #ifndef CONFIG_PM
  723. ret = rpr0521_als_enable(data, RPR0521_MODE_ALS_ENABLE);
  724. if (ret < 0)
  725. return ret;
  726. ret = rpr0521_pxs_enable(data, RPR0521_MODE_PXS_ENABLE);
  727. if (ret < 0)
  728. return ret;
  729. #endif
  730. data->irq_timestamp = 0;
  731. return 0;
  732. }
  733. static int rpr0521_poweroff(struct rpr0521_data *data)
  734. {
  735. int ret;
  736. int tmp;
  737. ret = regmap_update_bits(data->regmap, RPR0521_REG_MODE_CTRL,
  738. RPR0521_MODE_ALS_MASK |
  739. RPR0521_MODE_PXS_MASK,
  740. RPR0521_MODE_ALS_DISABLE |
  741. RPR0521_MODE_PXS_DISABLE);
  742. if (ret < 0)
  743. return ret;
  744. data->als_dev_en = false;
  745. data->pxs_dev_en = false;
  746. /*
  747. * Int pin keeps state after power off. Set pin to high impedance
  748. * mode to prevent power drain.
  749. */
  750. ret = regmap_read(data->regmap, RPR0521_REG_INTERRUPT, &tmp);
  751. if (ret) {
  752. dev_err(&data->client->dev, "Failed to reset int pin.\n");
  753. return ret;
  754. }
  755. return 0;
  756. }
  757. static bool rpr0521_is_volatile_reg(struct device *dev, unsigned int reg)
  758. {
  759. switch (reg) {
  760. case RPR0521_REG_MODE_CTRL:
  761. case RPR0521_REG_ALS_CTRL:
  762. case RPR0521_REG_PXS_CTRL:
  763. return false;
  764. default:
  765. return true;
  766. }
  767. }
  768. static const struct regmap_config rpr0521_regmap_config = {
  769. .name = RPR0521_REGMAP_NAME,
  770. .reg_bits = 8,
  771. .val_bits = 8,
  772. .max_register = RPR0521_REG_ID,
  773. .cache_type = REGCACHE_RBTREE,
  774. .volatile_reg = rpr0521_is_volatile_reg,
  775. };
  776. static int rpr0521_probe(struct i2c_client *client,
  777. const struct i2c_device_id *id)
  778. {
  779. struct rpr0521_data *data;
  780. struct iio_dev *indio_dev;
  781. struct regmap *regmap;
  782. int ret;
  783. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  784. if (!indio_dev)
  785. return -ENOMEM;
  786. regmap = devm_regmap_init_i2c(client, &rpr0521_regmap_config);
  787. if (IS_ERR(regmap)) {
  788. dev_err(&client->dev, "regmap_init failed!\n");
  789. return PTR_ERR(regmap);
  790. }
  791. data = iio_priv(indio_dev);
  792. i2c_set_clientdata(client, indio_dev);
  793. data->client = client;
  794. data->regmap = regmap;
  795. mutex_init(&data->lock);
  796. indio_dev->dev.parent = &client->dev;
  797. indio_dev->info = &rpr0521_info;
  798. indio_dev->name = RPR0521_DRV_NAME;
  799. indio_dev->channels = rpr0521_channels;
  800. indio_dev->num_channels = ARRAY_SIZE(rpr0521_channels);
  801. indio_dev->modes = INDIO_DIRECT_MODE;
  802. ret = rpr0521_init(data);
  803. if (ret < 0) {
  804. dev_err(&client->dev, "rpr0521 chip init failed\n");
  805. return ret;
  806. }
  807. ret = pm_runtime_set_active(&client->dev);
  808. if (ret < 0)
  809. goto err_poweroff;
  810. pm_runtime_enable(&client->dev);
  811. pm_runtime_set_autosuspend_delay(&client->dev, RPR0521_SLEEP_DELAY_MS);
  812. pm_runtime_use_autosuspend(&client->dev);
  813. /*
  814. * If sensor write/read is needed in _probe after _use_autosuspend,
  815. * sensor needs to be _resumed first using rpr0521_set_power_state().
  816. */
  817. /* IRQ to trigger setup */
  818. if (client->irq) {
  819. /* Trigger0 producer setup */
  820. data->drdy_trigger0 = devm_iio_trigger_alloc(
  821. indio_dev->dev.parent,
  822. "%s-dev%d", indio_dev->name, indio_dev->id);
  823. if (!data->drdy_trigger0) {
  824. ret = -ENOMEM;
  825. goto err_pm_disable;
  826. }
  827. data->drdy_trigger0->dev.parent = indio_dev->dev.parent;
  828. data->drdy_trigger0->ops = &rpr0521_trigger_ops;
  829. indio_dev->available_scan_masks = rpr0521_available_scan_masks;
  830. iio_trigger_set_drvdata(data->drdy_trigger0, indio_dev);
  831. /* Ties irq to trigger producer handler. */
  832. ret = devm_request_threaded_irq(&client->dev, client->irq,
  833. rpr0521_drdy_irq_handler, rpr0521_drdy_irq_thread,
  834. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  835. RPR0521_IRQ_NAME, indio_dev);
  836. if (ret < 0) {
  837. dev_err(&client->dev, "request irq %d for trigger0 failed\n",
  838. client->irq);
  839. goto err_pm_disable;
  840. }
  841. ret = devm_iio_trigger_register(indio_dev->dev.parent,
  842. data->drdy_trigger0);
  843. if (ret) {
  844. dev_err(&client->dev, "iio trigger register failed\n");
  845. goto err_pm_disable;
  846. }
  847. /*
  848. * Now whole pipe from physical interrupt (irq defined by
  849. * devicetree to device) to trigger0 output is set up.
  850. */
  851. /* Trigger consumer setup */
  852. ret = devm_iio_triggered_buffer_setup(indio_dev->dev.parent,
  853. indio_dev,
  854. rpr0521_trigger_consumer_store_time,
  855. rpr0521_trigger_consumer_handler,
  856. &rpr0521_buffer_setup_ops);
  857. if (ret < 0) {
  858. dev_err(&client->dev, "iio triggered buffer setup failed\n");
  859. goto err_pm_disable;
  860. }
  861. }
  862. ret = iio_device_register(indio_dev);
  863. if (ret)
  864. goto err_pm_disable;
  865. return 0;
  866. err_pm_disable:
  867. pm_runtime_disable(&client->dev);
  868. pm_runtime_set_suspended(&client->dev);
  869. pm_runtime_put_noidle(&client->dev);
  870. err_poweroff:
  871. rpr0521_poweroff(data);
  872. return ret;
  873. }
  874. static int rpr0521_remove(struct i2c_client *client)
  875. {
  876. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  877. iio_device_unregister(indio_dev);
  878. pm_runtime_disable(&client->dev);
  879. pm_runtime_set_suspended(&client->dev);
  880. pm_runtime_put_noidle(&client->dev);
  881. rpr0521_poweroff(iio_priv(indio_dev));
  882. return 0;
  883. }
  884. #ifdef CONFIG_PM
  885. static int rpr0521_runtime_suspend(struct device *dev)
  886. {
  887. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  888. struct rpr0521_data *data = iio_priv(indio_dev);
  889. int ret;
  890. mutex_lock(&data->lock);
  891. /* If measurements are enabled, enable them on resume */
  892. if (!data->als_need_dis)
  893. data->als_ps_need_en = data->als_dev_en;
  894. if (!data->pxs_need_dis)
  895. data->pxs_ps_need_en = data->pxs_dev_en;
  896. /* disable channels and sets {als,pxs}_dev_en to false */
  897. ret = rpr0521_poweroff(data);
  898. regcache_mark_dirty(data->regmap);
  899. mutex_unlock(&data->lock);
  900. return ret;
  901. }
  902. static int rpr0521_runtime_resume(struct device *dev)
  903. {
  904. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  905. struct rpr0521_data *data = iio_priv(indio_dev);
  906. int ret;
  907. regcache_sync(data->regmap);
  908. if (data->als_ps_need_en) {
  909. ret = rpr0521_als_enable(data, RPR0521_MODE_ALS_ENABLE);
  910. if (ret < 0)
  911. return ret;
  912. data->als_ps_need_en = false;
  913. }
  914. if (data->pxs_ps_need_en) {
  915. ret = rpr0521_pxs_enable(data, RPR0521_MODE_PXS_ENABLE);
  916. if (ret < 0)
  917. return ret;
  918. data->pxs_ps_need_en = false;
  919. }
  920. msleep(100); //wait for first measurement result
  921. return 0;
  922. }
  923. #endif
  924. static const struct dev_pm_ops rpr0521_pm_ops = {
  925. SET_RUNTIME_PM_OPS(rpr0521_runtime_suspend,
  926. rpr0521_runtime_resume, NULL)
  927. };
  928. static const struct acpi_device_id rpr0521_acpi_match[] = {
  929. {"RPR0521", 0},
  930. { }
  931. };
  932. MODULE_DEVICE_TABLE(acpi, rpr0521_acpi_match);
  933. static const struct i2c_device_id rpr0521_id[] = {
  934. {"rpr0521", 0},
  935. { }
  936. };
  937. MODULE_DEVICE_TABLE(i2c, rpr0521_id);
  938. static struct i2c_driver rpr0521_driver = {
  939. .driver = {
  940. .name = RPR0521_DRV_NAME,
  941. .pm = &rpr0521_pm_ops,
  942. .acpi_match_table = ACPI_PTR(rpr0521_acpi_match),
  943. },
  944. .probe = rpr0521_probe,
  945. .remove = rpr0521_remove,
  946. .id_table = rpr0521_id,
  947. };
  948. module_i2c_driver(rpr0521_driver);
  949. MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
  950. MODULE_DESCRIPTION("RPR0521 ROHM Ambient Light and Proximity Sensor driver");
  951. MODULE_LICENSE("GPL v2");