apds9960.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /*
  2. * apds9960.c - Support for Avago APDS9960 gesture/RGB/ALS/proximity sensor
  3. *
  4. * Copyright (C) 2015 Matt Ranostay <mranostay@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * TODO: gesture + proximity calib offsets
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/delay.h>
  22. #include <linux/mutex.h>
  23. #include <linux/err.h>
  24. #include <linux/irq.h>
  25. #include <linux/gpio.h>
  26. #include <linux/i2c.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/regmap.h>
  29. #include <linux/iio/iio.h>
  30. #include <linux/iio/buffer.h>
  31. #include <linux/iio/events.h>
  32. #include <linux/iio/kfifo_buf.h>
  33. #include <linux/iio/sysfs.h>
  34. #include <linux/of_gpio.h>
  35. #define APDS9960_REGMAP_NAME "apds9960_regmap"
  36. #define APDS9960_DRV_NAME "apds9960"
  37. #define APDS9960_REG_RAM_START 0x00
  38. #define APDS9960_REG_RAM_END 0x7f
  39. #define APDS9960_REG_ENABLE 0x80
  40. #define APDS9960_REG_ATIME 0x81
  41. #define APDS9960_REG_WTIME 0x83
  42. #define APDS9960_REG_AILTL 0x84
  43. #define APDS9960_REG_AILTH 0x85
  44. #define APDS9960_REG_AIHTL 0x86
  45. #define APDS9960_REG_AIHTH 0x87
  46. #define APDS9960_REG_PILT 0x89
  47. #define APDS9960_REG_PIHT 0x8b
  48. #define APDS9960_REG_PERS 0x8c
  49. #define APDS9960_REG_CONFIG_1 0x8d
  50. #define APDS9960_REG_PPULSE 0x8e
  51. #define APDS9960_REG_CONTROL 0x8f
  52. #define APDS9960_REG_CONTROL_AGAIN_MASK 0x03
  53. #define APDS9960_REG_CONTROL_PGAIN_MASK 0x0c
  54. #define APDS9960_REG_CONTROL_AGAIN_MASK_SHIFT 0
  55. #define APDS9960_REG_CONTROL_PGAIN_MASK_SHIFT 2
  56. #define APDS9960_REG_CONFIG_2 0x90
  57. #define APDS9960_REG_CONFIG_2_GGAIN_MASK 0x60
  58. #define APDS9960_REG_CONFIG_2_GGAIN_MASK_SHIFT 5
  59. #define APDS9960_REG_ID 0x92
  60. #define APDS9960_REG_STATUS 0x93
  61. #define APDS9960_REG_STATUS_PS_INT BIT(5)
  62. #define APDS9960_REG_STATUS_ALS_INT BIT(4)
  63. #define APDS9960_REG_STATUS_GINT BIT(2)
  64. #define APDS9960_REG_PDATA 0x9c
  65. #define APDS9960_REG_POFFSET_UR 0x9d
  66. #define APDS9960_REG_POFFSET_DL 0x9e
  67. #define APDS9960_REG_CONFIG_3 0x9f
  68. #define APDS9960_REG_GPENTH 0xa0
  69. #define APDS9960_REG_GEXTH 0xa1
  70. #define APDS9960_REG_GCONF_1 0xa2
  71. #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK 0xc0
  72. #define APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT 6
  73. #define APDS9960_REG_GCONF_2 0xa3
  74. #define APDS9960_REG_GOFFSET_U 0xa4
  75. #define APDS9960_REG_GOFFSET_D 0xa5
  76. #define APDS9960_REG_GPULSE 0xa6
  77. #define APDS9960_REG_GOFFSET_L 0xa7
  78. #define APDS9960_REG_GOFFSET_R 0xa9
  79. #define APDS9960_REG_GCONF_3 0xaa
  80. #define APDS9960_REG_GCONF_4 0xab
  81. #define APDS9960_REG_GFLVL 0xae
  82. #define APDS9960_REG_GSTATUS 0xaf
  83. #define APDS9960_REG_IFORCE 0xe4
  84. #define APDS9960_REG_PICLEAR 0xe5
  85. #define APDS9960_REG_CICLEAR 0xe6
  86. #define APDS9960_REG_AICLEAR 0xe7
  87. #define APDS9960_DEFAULT_PERS 0x33
  88. #define APDS9960_DEFAULT_GPENTH 0x50
  89. #define APDS9960_DEFAULT_GEXTH 0x40
  90. #define APDS9960_MAX_PXS_THRES_VAL 255
  91. #define APDS9960_MAX_ALS_THRES_VAL 0xffff
  92. #define APDS9960_MAX_INT_TIME_IN_US 1000000
  93. enum apds9960_als_channel_idx {
  94. IDX_ALS_CLEAR, IDX_ALS_RED, IDX_ALS_GREEN, IDX_ALS_BLUE,
  95. };
  96. #define APDS9960_REG_ALS_BASE 0x94
  97. #define APDS9960_REG_ALS_CHANNEL(_colour) \
  98. (APDS9960_REG_ALS_BASE + (IDX_ALS_##_colour * 2))
  99. enum apds9960_gesture_channel_idx {
  100. IDX_DIR_UP, IDX_DIR_DOWN, IDX_DIR_LEFT, IDX_DIR_RIGHT,
  101. };
  102. #define APDS9960_REG_GFIFO_BASE 0xfc
  103. #define APDS9960_REG_GFIFO_DIR(_dir) \
  104. (APDS9960_REG_GFIFO_BASE + IDX_DIR_##_dir)
  105. struct apds9960_data {
  106. struct i2c_client *client;
  107. struct iio_dev *indio_dev;
  108. struct mutex lock;
  109. /* regmap fields */
  110. struct regmap *regmap;
  111. struct regmap_field *reg_int_als;
  112. struct regmap_field *reg_int_ges;
  113. struct regmap_field *reg_int_pxs;
  114. struct regmap_field *reg_enable_als;
  115. struct regmap_field *reg_enable_ges;
  116. struct regmap_field *reg_enable_pxs;
  117. /* state */
  118. int als_int;
  119. int pxs_int;
  120. int gesture_mode_running;
  121. /* gain values */
  122. int als_gain;
  123. int pxs_gain;
  124. /* integration time value in us */
  125. int als_adc_int_us;
  126. /* gesture buffer */
  127. u8 buffer[4]; /* 4 8-bit channels */
  128. };
  129. static const struct reg_default apds9960_reg_defaults[] = {
  130. /* Default ALS integration time = 2.48ms */
  131. { APDS9960_REG_ATIME, 0xff },
  132. };
  133. static const struct regmap_range apds9960_volatile_ranges[] = {
  134. regmap_reg_range(APDS9960_REG_STATUS,
  135. APDS9960_REG_PDATA),
  136. regmap_reg_range(APDS9960_REG_GFLVL,
  137. APDS9960_REG_GSTATUS),
  138. regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP),
  139. APDS9960_REG_GFIFO_DIR(RIGHT)),
  140. regmap_reg_range(APDS9960_REG_IFORCE,
  141. APDS9960_REG_AICLEAR),
  142. };
  143. static const struct regmap_access_table apds9960_volatile_table = {
  144. .yes_ranges = apds9960_volatile_ranges,
  145. .n_yes_ranges = ARRAY_SIZE(apds9960_volatile_ranges),
  146. };
  147. static const struct regmap_range apds9960_precious_ranges[] = {
  148. regmap_reg_range(APDS9960_REG_RAM_START, APDS9960_REG_RAM_END),
  149. };
  150. static const struct regmap_access_table apds9960_precious_table = {
  151. .yes_ranges = apds9960_precious_ranges,
  152. .n_yes_ranges = ARRAY_SIZE(apds9960_precious_ranges),
  153. };
  154. static const struct regmap_range apds9960_readable_ranges[] = {
  155. regmap_reg_range(APDS9960_REG_ENABLE,
  156. APDS9960_REG_GSTATUS),
  157. regmap_reg_range(APDS9960_REG_GFIFO_DIR(UP),
  158. APDS9960_REG_GFIFO_DIR(RIGHT)),
  159. };
  160. static const struct regmap_access_table apds9960_readable_table = {
  161. .yes_ranges = apds9960_readable_ranges,
  162. .n_yes_ranges = ARRAY_SIZE(apds9960_readable_ranges),
  163. };
  164. static const struct regmap_range apds9960_writeable_ranges[] = {
  165. regmap_reg_range(APDS9960_REG_ENABLE, APDS9960_REG_CONFIG_2),
  166. regmap_reg_range(APDS9960_REG_POFFSET_UR, APDS9960_REG_GCONF_4),
  167. regmap_reg_range(APDS9960_REG_IFORCE, APDS9960_REG_AICLEAR),
  168. };
  169. static const struct regmap_access_table apds9960_writeable_table = {
  170. .yes_ranges = apds9960_writeable_ranges,
  171. .n_yes_ranges = ARRAY_SIZE(apds9960_writeable_ranges),
  172. };
  173. static const struct regmap_config apds9960_regmap_config = {
  174. .name = APDS9960_REGMAP_NAME,
  175. .reg_bits = 8,
  176. .val_bits = 8,
  177. .use_single_rw = 1,
  178. .volatile_table = &apds9960_volatile_table,
  179. .precious_table = &apds9960_precious_table,
  180. .rd_table = &apds9960_readable_table,
  181. .wr_table = &apds9960_writeable_table,
  182. .reg_defaults = apds9960_reg_defaults,
  183. .num_reg_defaults = ARRAY_SIZE(apds9960_reg_defaults),
  184. .max_register = APDS9960_REG_GFIFO_DIR(RIGHT),
  185. .cache_type = REGCACHE_RBTREE,
  186. };
  187. static const struct iio_event_spec apds9960_pxs_event_spec[] = {
  188. {
  189. .type = IIO_EV_TYPE_THRESH,
  190. .dir = IIO_EV_DIR_RISING,
  191. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  192. BIT(IIO_EV_INFO_ENABLE),
  193. },
  194. {
  195. .type = IIO_EV_TYPE_THRESH,
  196. .dir = IIO_EV_DIR_FALLING,
  197. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  198. BIT(IIO_EV_INFO_ENABLE),
  199. },
  200. };
  201. static const struct iio_event_spec apds9960_als_event_spec[] = {
  202. {
  203. .type = IIO_EV_TYPE_THRESH,
  204. .dir = IIO_EV_DIR_RISING,
  205. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  206. BIT(IIO_EV_INFO_ENABLE),
  207. },
  208. {
  209. .type = IIO_EV_TYPE_THRESH,
  210. .dir = IIO_EV_DIR_FALLING,
  211. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  212. BIT(IIO_EV_INFO_ENABLE),
  213. },
  214. };
  215. #define APDS9960_GESTURE_CHANNEL(_dir, _si) { \
  216. .type = IIO_PROXIMITY, \
  217. .channel = _si + 1, \
  218. .scan_index = _si, \
  219. .indexed = 1, \
  220. .scan_type = { \
  221. .sign = 'u', \
  222. .realbits = 8, \
  223. .storagebits = 8, \
  224. }, \
  225. }
  226. #define APDS9960_INTENSITY_CHANNEL(_colour) { \
  227. .type = IIO_INTENSITY, \
  228. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  229. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
  230. BIT(IIO_CHAN_INFO_INT_TIME), \
  231. .channel2 = IIO_MOD_LIGHT_##_colour, \
  232. .address = APDS9960_REG_ALS_CHANNEL(_colour), \
  233. .modified = 1, \
  234. .scan_index = -1, \
  235. }
  236. static const unsigned long apds9960_scan_masks[] = {0xf, 0};
  237. static const struct iio_chan_spec apds9960_channels[] = {
  238. {
  239. .type = IIO_PROXIMITY,
  240. .address = APDS9960_REG_PDATA,
  241. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  242. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
  243. .channel = 0,
  244. .indexed = 0,
  245. .scan_index = -1,
  246. .event_spec = apds9960_pxs_event_spec,
  247. .num_event_specs = ARRAY_SIZE(apds9960_pxs_event_spec),
  248. },
  249. /* Gesture Sensor */
  250. APDS9960_GESTURE_CHANNEL(UP, 0),
  251. APDS9960_GESTURE_CHANNEL(DOWN, 1),
  252. APDS9960_GESTURE_CHANNEL(LEFT, 2),
  253. APDS9960_GESTURE_CHANNEL(RIGHT, 3),
  254. /* ALS */
  255. {
  256. .type = IIO_INTENSITY,
  257. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
  258. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
  259. BIT(IIO_CHAN_INFO_INT_TIME),
  260. .channel2 = IIO_MOD_LIGHT_CLEAR,
  261. .address = APDS9960_REG_ALS_CHANNEL(CLEAR),
  262. .modified = 1,
  263. .scan_index = -1,
  264. .event_spec = apds9960_als_event_spec,
  265. .num_event_specs = ARRAY_SIZE(apds9960_als_event_spec),
  266. },
  267. /* RGB Sensor */
  268. APDS9960_INTENSITY_CHANNEL(RED),
  269. APDS9960_INTENSITY_CHANNEL(GREEN),
  270. APDS9960_INTENSITY_CHANNEL(BLUE),
  271. };
  272. /* integration time in us */
  273. static const int apds9960_int_time[][2] = {
  274. { 28000, 246},
  275. {100000, 219},
  276. {200000, 182},
  277. {700000, 0}
  278. };
  279. /* gain mapping */
  280. static const int apds9960_pxs_gain_map[] = {1, 2, 4, 8};
  281. static const int apds9960_als_gain_map[] = {1, 4, 16, 64};
  282. static IIO_CONST_ATTR(proximity_scale_available, "1 2 4 8");
  283. static IIO_CONST_ATTR(intensity_scale_available, "1 4 16 64");
  284. static IIO_CONST_ATTR_INT_TIME_AVAIL("0.028 0.1 0.2 0.7");
  285. static struct attribute *apds9960_attributes[] = {
  286. &iio_const_attr_proximity_scale_available.dev_attr.attr,
  287. &iio_const_attr_intensity_scale_available.dev_attr.attr,
  288. &iio_const_attr_integration_time_available.dev_attr.attr,
  289. NULL,
  290. };
  291. static struct attribute_group apds9960_attribute_group = {
  292. .attrs = apds9960_attributes,
  293. };
  294. static const struct reg_field apds9960_reg_field_int_als =
  295. REG_FIELD(APDS9960_REG_ENABLE, 4, 4);
  296. static const struct reg_field apds9960_reg_field_int_ges =
  297. REG_FIELD(APDS9960_REG_GCONF_4, 1, 1);
  298. static const struct reg_field apds9960_reg_field_int_pxs =
  299. REG_FIELD(APDS9960_REG_ENABLE, 5, 5);
  300. static const struct reg_field apds9960_reg_field_enable_als =
  301. REG_FIELD(APDS9960_REG_ENABLE, 1, 1);
  302. static const struct reg_field apds9960_reg_field_enable_ges =
  303. REG_FIELD(APDS9960_REG_ENABLE, 6, 6);
  304. static const struct reg_field apds9960_reg_field_enable_pxs =
  305. REG_FIELD(APDS9960_REG_ENABLE, 2, 2);
  306. static int apds9960_set_it_time(struct apds9960_data *data, int val2)
  307. {
  308. int ret = -EINVAL;
  309. int idx;
  310. for (idx = 0; idx < ARRAY_SIZE(apds9960_int_time); idx++) {
  311. if (apds9960_int_time[idx][0] == val2) {
  312. mutex_lock(&data->lock);
  313. ret = regmap_write(data->regmap, APDS9960_REG_ATIME,
  314. apds9960_int_time[idx][1]);
  315. if (!ret)
  316. data->als_adc_int_us = val2;
  317. mutex_unlock(&data->lock);
  318. break;
  319. }
  320. }
  321. return ret;
  322. }
  323. static int apds9960_set_pxs_gain(struct apds9960_data *data, int val)
  324. {
  325. int ret = -EINVAL;
  326. int idx;
  327. for (idx = 0; idx < ARRAY_SIZE(apds9960_pxs_gain_map); idx++) {
  328. if (apds9960_pxs_gain_map[idx] == val) {
  329. /* pxs + gesture gains are mirrored */
  330. mutex_lock(&data->lock);
  331. ret = regmap_update_bits(data->regmap,
  332. APDS9960_REG_CONTROL,
  333. APDS9960_REG_CONTROL_PGAIN_MASK,
  334. idx << APDS9960_REG_CONTROL_PGAIN_MASK_SHIFT);
  335. if (ret) {
  336. mutex_unlock(&data->lock);
  337. break;
  338. }
  339. ret = regmap_update_bits(data->regmap,
  340. APDS9960_REG_CONFIG_2,
  341. APDS9960_REG_CONFIG_2_GGAIN_MASK,
  342. idx << APDS9960_REG_CONFIG_2_GGAIN_MASK_SHIFT);
  343. if (!ret)
  344. data->pxs_gain = idx;
  345. mutex_unlock(&data->lock);
  346. break;
  347. }
  348. }
  349. return ret;
  350. }
  351. static int apds9960_set_als_gain(struct apds9960_data *data, int val)
  352. {
  353. int ret = -EINVAL;
  354. int idx;
  355. for (idx = 0; idx < ARRAY_SIZE(apds9960_als_gain_map); idx++) {
  356. if (apds9960_als_gain_map[idx] == val) {
  357. mutex_lock(&data->lock);
  358. ret = regmap_update_bits(data->regmap,
  359. APDS9960_REG_CONTROL,
  360. APDS9960_REG_CONTROL_AGAIN_MASK, idx);
  361. if (!ret)
  362. data->als_gain = idx;
  363. mutex_unlock(&data->lock);
  364. break;
  365. }
  366. }
  367. return ret;
  368. }
  369. #ifdef CONFIG_PM
  370. static int apds9960_set_power_state(struct apds9960_data *data, bool on)
  371. {
  372. struct device *dev = &data->client->dev;
  373. int ret = 0;
  374. mutex_lock(&data->lock);
  375. if (on) {
  376. int suspended;
  377. suspended = pm_runtime_suspended(dev);
  378. ret = pm_runtime_get_sync(dev);
  379. /* Allow one integration cycle before allowing a reading */
  380. if (suspended)
  381. usleep_range(data->als_adc_int_us,
  382. APDS9960_MAX_INT_TIME_IN_US);
  383. } else {
  384. pm_runtime_mark_last_busy(dev);
  385. ret = pm_runtime_put_autosuspend(dev);
  386. }
  387. mutex_unlock(&data->lock);
  388. return ret;
  389. }
  390. #else
  391. static int apds9960_set_power_state(struct apds9960_data *data, bool on)
  392. {
  393. return 0;
  394. }
  395. #endif
  396. static int apds9960_read_raw(struct iio_dev *indio_dev,
  397. struct iio_chan_spec const *chan,
  398. int *val, int *val2, long mask)
  399. {
  400. struct apds9960_data *data = iio_priv(indio_dev);
  401. __le16 buf;
  402. int ret = -EINVAL;
  403. if (data->gesture_mode_running)
  404. return -EBUSY;
  405. switch (mask) {
  406. case IIO_CHAN_INFO_RAW:
  407. apds9960_set_power_state(data, true);
  408. switch (chan->type) {
  409. case IIO_PROXIMITY:
  410. ret = regmap_read(data->regmap, chan->address, val);
  411. if (!ret)
  412. ret = IIO_VAL_INT;
  413. break;
  414. case IIO_INTENSITY:
  415. ret = regmap_bulk_read(data->regmap, chan->address,
  416. &buf, 2);
  417. if (!ret) {
  418. ret = IIO_VAL_INT;
  419. *val = le16_to_cpu(buf);
  420. }
  421. break;
  422. default:
  423. ret = -EINVAL;
  424. }
  425. apds9960_set_power_state(data, false);
  426. break;
  427. case IIO_CHAN_INFO_INT_TIME:
  428. /* RGB + ALS sensors only have integration time */
  429. mutex_lock(&data->lock);
  430. switch (chan->type) {
  431. case IIO_INTENSITY:
  432. *val = 0;
  433. *val2 = data->als_adc_int_us;
  434. ret = IIO_VAL_INT_PLUS_MICRO;
  435. break;
  436. default:
  437. ret = -EINVAL;
  438. }
  439. mutex_unlock(&data->lock);
  440. break;
  441. case IIO_CHAN_INFO_SCALE:
  442. mutex_lock(&data->lock);
  443. switch (chan->type) {
  444. case IIO_PROXIMITY:
  445. *val = apds9960_pxs_gain_map[data->pxs_gain];
  446. ret = IIO_VAL_INT;
  447. break;
  448. case IIO_INTENSITY:
  449. *val = apds9960_als_gain_map[data->als_gain];
  450. ret = IIO_VAL_INT;
  451. break;
  452. default:
  453. ret = -EINVAL;
  454. }
  455. mutex_unlock(&data->lock);
  456. break;
  457. }
  458. return ret;
  459. };
  460. static int apds9960_write_raw(struct iio_dev *indio_dev,
  461. struct iio_chan_spec const *chan,
  462. int val, int val2, long mask)
  463. {
  464. struct apds9960_data *data = iio_priv(indio_dev);
  465. switch (mask) {
  466. case IIO_CHAN_INFO_INT_TIME:
  467. /* RGB + ALS sensors only have int time */
  468. switch (chan->type) {
  469. case IIO_INTENSITY:
  470. if (val != 0)
  471. return -EINVAL;
  472. return apds9960_set_it_time(data, val2);
  473. default:
  474. return -EINVAL;
  475. }
  476. case IIO_CHAN_INFO_SCALE:
  477. if (val2 != 0)
  478. return -EINVAL;
  479. switch (chan->type) {
  480. case IIO_PROXIMITY:
  481. return apds9960_set_pxs_gain(data, val);
  482. case IIO_INTENSITY:
  483. return apds9960_set_als_gain(data, val);
  484. default:
  485. return -EINVAL;
  486. }
  487. default:
  488. return -EINVAL;
  489. };
  490. return 0;
  491. }
  492. static inline int apds9960_get_thres_reg(const struct iio_chan_spec *chan,
  493. enum iio_event_direction dir,
  494. u8 *reg)
  495. {
  496. switch (dir) {
  497. case IIO_EV_DIR_RISING:
  498. switch (chan->type) {
  499. case IIO_PROXIMITY:
  500. *reg = APDS9960_REG_PIHT;
  501. break;
  502. case IIO_INTENSITY:
  503. *reg = APDS9960_REG_AIHTL;
  504. break;
  505. default:
  506. return -EINVAL;
  507. }
  508. break;
  509. case IIO_EV_DIR_FALLING:
  510. switch (chan->type) {
  511. case IIO_PROXIMITY:
  512. *reg = APDS9960_REG_PILT;
  513. break;
  514. case IIO_INTENSITY:
  515. *reg = APDS9960_REG_AILTL;
  516. break;
  517. default:
  518. return -EINVAL;
  519. }
  520. break;
  521. default:
  522. return -EINVAL;
  523. }
  524. return 0;
  525. }
  526. static int apds9960_read_event(struct iio_dev *indio_dev,
  527. const struct iio_chan_spec *chan,
  528. enum iio_event_type type,
  529. enum iio_event_direction dir,
  530. enum iio_event_info info,
  531. int *val, int *val2)
  532. {
  533. u8 reg;
  534. __le16 buf;
  535. int ret = 0;
  536. struct apds9960_data *data = iio_priv(indio_dev);
  537. if (info != IIO_EV_INFO_VALUE)
  538. return -EINVAL;
  539. ret = apds9960_get_thres_reg(chan, dir, &reg);
  540. if (ret < 0)
  541. return ret;
  542. if (chan->type == IIO_PROXIMITY) {
  543. ret = regmap_read(data->regmap, reg, val);
  544. if (ret < 0)
  545. return ret;
  546. } else if (chan->type == IIO_INTENSITY) {
  547. ret = regmap_bulk_read(data->regmap, reg, &buf, 2);
  548. if (ret < 0)
  549. return ret;
  550. *val = le16_to_cpu(buf);
  551. } else
  552. return -EINVAL;
  553. *val2 = 0;
  554. return IIO_VAL_INT;
  555. }
  556. static int apds9960_write_event(struct iio_dev *indio_dev,
  557. const struct iio_chan_spec *chan,
  558. enum iio_event_type type,
  559. enum iio_event_direction dir,
  560. enum iio_event_info info,
  561. int val, int val2)
  562. {
  563. u8 reg;
  564. __le16 buf;
  565. int ret = 0;
  566. struct apds9960_data *data = iio_priv(indio_dev);
  567. if (info != IIO_EV_INFO_VALUE)
  568. return -EINVAL;
  569. ret = apds9960_get_thres_reg(chan, dir, &reg);
  570. if (ret < 0)
  571. return ret;
  572. if (chan->type == IIO_PROXIMITY) {
  573. if (val < 0 || val > APDS9960_MAX_PXS_THRES_VAL)
  574. return -EINVAL;
  575. ret = regmap_write(data->regmap, reg, val);
  576. if (ret < 0)
  577. return ret;
  578. } else if (chan->type == IIO_INTENSITY) {
  579. if (val < 0 || val > APDS9960_MAX_ALS_THRES_VAL)
  580. return -EINVAL;
  581. buf = cpu_to_le16(val);
  582. ret = regmap_bulk_write(data->regmap, reg, &buf, 2);
  583. if (ret < 0)
  584. return ret;
  585. } else
  586. return -EINVAL;
  587. return 0;
  588. }
  589. static int apds9960_read_event_config(struct iio_dev *indio_dev,
  590. const struct iio_chan_spec *chan,
  591. enum iio_event_type type,
  592. enum iio_event_direction dir)
  593. {
  594. struct apds9960_data *data = iio_priv(indio_dev);
  595. switch (chan->type) {
  596. case IIO_PROXIMITY:
  597. return data->pxs_int;
  598. case IIO_INTENSITY:
  599. return data->als_int;
  600. default:
  601. return -EINVAL;
  602. }
  603. return 0;
  604. }
  605. static int apds9960_write_event_config(struct iio_dev *indio_dev,
  606. const struct iio_chan_spec *chan,
  607. enum iio_event_type type,
  608. enum iio_event_direction dir,
  609. int state)
  610. {
  611. struct apds9960_data *data = iio_priv(indio_dev);
  612. int ret;
  613. state = !!state;
  614. switch (chan->type) {
  615. case IIO_PROXIMITY:
  616. if (data->pxs_int == state)
  617. return -EINVAL;
  618. ret = regmap_field_write(data->reg_int_pxs, state);
  619. if (ret)
  620. return ret;
  621. data->pxs_int = state;
  622. apds9960_set_power_state(data, state);
  623. break;
  624. case IIO_INTENSITY:
  625. if (data->als_int == state)
  626. return -EINVAL;
  627. ret = regmap_field_write(data->reg_int_als, state);
  628. if (ret)
  629. return ret;
  630. data->als_int = state;
  631. apds9960_set_power_state(data, state);
  632. break;
  633. default:
  634. return -EINVAL;
  635. }
  636. return 0;
  637. }
  638. static const struct iio_info apds9960_info = {
  639. .driver_module = THIS_MODULE,
  640. .attrs = &apds9960_attribute_group,
  641. .read_raw = apds9960_read_raw,
  642. .write_raw = apds9960_write_raw,
  643. .read_event_value = apds9960_read_event,
  644. .write_event_value = apds9960_write_event,
  645. .read_event_config = apds9960_read_event_config,
  646. .write_event_config = apds9960_write_event_config,
  647. };
  648. static inline int apds9660_fifo_is_empty(struct apds9960_data *data)
  649. {
  650. int cnt;
  651. int ret;
  652. ret = regmap_read(data->regmap, APDS9960_REG_GFLVL, &cnt);
  653. if (ret)
  654. return ret;
  655. return cnt;
  656. }
  657. static void apds9960_read_gesture_fifo(struct apds9960_data *data)
  658. {
  659. int ret, cnt = 0;
  660. mutex_lock(&data->lock);
  661. data->gesture_mode_running = 1;
  662. while (cnt || (cnt = apds9660_fifo_is_empty(data) > 0)) {
  663. ret = regmap_bulk_read(data->regmap, APDS9960_REG_GFIFO_BASE,
  664. &data->buffer, 4);
  665. if (ret)
  666. goto err_read;
  667. iio_push_to_buffers(data->indio_dev, data->buffer);
  668. cnt--;
  669. }
  670. err_read:
  671. data->gesture_mode_running = 0;
  672. mutex_unlock(&data->lock);
  673. }
  674. static irqreturn_t apds9960_interrupt_handler(int irq, void *private)
  675. {
  676. struct iio_dev *indio_dev = private;
  677. struct apds9960_data *data = iio_priv(indio_dev);
  678. int ret, status;
  679. ret = regmap_read(data->regmap, APDS9960_REG_STATUS, &status);
  680. if (ret < 0) {
  681. dev_err(&data->client->dev, "irq status reg read failed\n");
  682. return IRQ_HANDLED;
  683. }
  684. if ((status & APDS9960_REG_STATUS_ALS_INT) && data->als_int) {
  685. iio_push_event(indio_dev,
  686. IIO_UNMOD_EVENT_CODE(IIO_INTENSITY, 0,
  687. IIO_EV_TYPE_THRESH,
  688. IIO_EV_DIR_EITHER),
  689. iio_get_time_ns(indio_dev));
  690. regmap_write(data->regmap, APDS9960_REG_CICLEAR, 1);
  691. }
  692. if ((status & APDS9960_REG_STATUS_PS_INT) && data->pxs_int) {
  693. iio_push_event(indio_dev,
  694. IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, 0,
  695. IIO_EV_TYPE_THRESH,
  696. IIO_EV_DIR_EITHER),
  697. iio_get_time_ns(indio_dev));
  698. regmap_write(data->regmap, APDS9960_REG_PICLEAR, 1);
  699. }
  700. if (status & APDS9960_REG_STATUS_GINT)
  701. apds9960_read_gesture_fifo(data);
  702. return IRQ_HANDLED;
  703. }
  704. static int apds9960_set_powermode(struct apds9960_data *data, bool state)
  705. {
  706. return regmap_update_bits(data->regmap, APDS9960_REG_ENABLE, 1, state);
  707. }
  708. static int apds9960_buffer_postenable(struct iio_dev *indio_dev)
  709. {
  710. struct apds9960_data *data = iio_priv(indio_dev);
  711. int ret;
  712. ret = regmap_field_write(data->reg_int_ges, 1);
  713. if (ret)
  714. return ret;
  715. ret = regmap_field_write(data->reg_enable_ges, 1);
  716. if (ret)
  717. return ret;
  718. pm_runtime_get_sync(&data->client->dev);
  719. return 0;
  720. }
  721. static int apds9960_buffer_predisable(struct iio_dev *indio_dev)
  722. {
  723. struct apds9960_data *data = iio_priv(indio_dev);
  724. int ret;
  725. ret = regmap_field_write(data->reg_enable_ges, 0);
  726. if (ret)
  727. return ret;
  728. ret = regmap_field_write(data->reg_int_ges, 0);
  729. if (ret)
  730. return ret;
  731. pm_runtime_put_autosuspend(&data->client->dev);
  732. return 0;
  733. }
  734. static const struct iio_buffer_setup_ops apds9960_buffer_setup_ops = {
  735. .postenable = apds9960_buffer_postenable,
  736. .predisable = apds9960_buffer_predisable,
  737. };
  738. static int apds9960_regfield_init(struct apds9960_data *data)
  739. {
  740. struct device *dev = &data->client->dev;
  741. struct regmap *regmap = data->regmap;
  742. data->reg_int_als = devm_regmap_field_alloc(dev, regmap,
  743. apds9960_reg_field_int_als);
  744. if (IS_ERR(data->reg_int_als)) {
  745. dev_err(dev, "INT ALS reg field init failed\n");
  746. return PTR_ERR(data->reg_int_als);
  747. }
  748. data->reg_int_ges = devm_regmap_field_alloc(dev, regmap,
  749. apds9960_reg_field_int_ges);
  750. if (IS_ERR(data->reg_int_ges)) {
  751. dev_err(dev, "INT gesture reg field init failed\n");
  752. return PTR_ERR(data->reg_int_ges);
  753. }
  754. data->reg_int_pxs = devm_regmap_field_alloc(dev, regmap,
  755. apds9960_reg_field_int_pxs);
  756. if (IS_ERR(data->reg_int_pxs)) {
  757. dev_err(dev, "INT pxs reg field init failed\n");
  758. return PTR_ERR(data->reg_int_pxs);
  759. }
  760. data->reg_enable_als = devm_regmap_field_alloc(dev, regmap,
  761. apds9960_reg_field_enable_als);
  762. if (IS_ERR(data->reg_enable_als)) {
  763. dev_err(dev, "Enable ALS reg field init failed\n");
  764. return PTR_ERR(data->reg_enable_als);
  765. }
  766. data->reg_enable_ges = devm_regmap_field_alloc(dev, regmap,
  767. apds9960_reg_field_enable_ges);
  768. if (IS_ERR(data->reg_enable_ges)) {
  769. dev_err(dev, "Enable gesture reg field init failed\n");
  770. return PTR_ERR(data->reg_enable_ges);
  771. }
  772. data->reg_enable_pxs = devm_regmap_field_alloc(dev, regmap,
  773. apds9960_reg_field_enable_pxs);
  774. if (IS_ERR(data->reg_enable_pxs)) {
  775. dev_err(dev, "Enable PXS reg field init failed\n");
  776. return PTR_ERR(data->reg_enable_pxs);
  777. }
  778. return 0;
  779. }
  780. static int apds9960_chip_init(struct apds9960_data *data)
  781. {
  782. int ret;
  783. /* Default IT for ALS of 28 ms */
  784. ret = apds9960_set_it_time(data, 28000);
  785. if (ret)
  786. return ret;
  787. /* Ensure gesture interrupt is OFF */
  788. ret = regmap_field_write(data->reg_int_ges, 0);
  789. if (ret)
  790. return ret;
  791. /* Disable gesture sensor, since polling is useless from user-space */
  792. ret = regmap_field_write(data->reg_enable_ges, 0);
  793. if (ret)
  794. return ret;
  795. /* Ensure proximity interrupt is OFF */
  796. ret = regmap_field_write(data->reg_int_pxs, 0);
  797. if (ret)
  798. return ret;
  799. /* Enable proximity sensor for polling */
  800. ret = regmap_field_write(data->reg_enable_pxs, 1);
  801. if (ret)
  802. return ret;
  803. /* Ensure ALS interrupt is OFF */
  804. ret = regmap_field_write(data->reg_int_als, 0);
  805. if (ret)
  806. return ret;
  807. /* Enable ALS sensor for polling */
  808. ret = regmap_field_write(data->reg_enable_als, 1);
  809. if (ret)
  810. return ret;
  811. /*
  812. * When enabled trigger an interrupt after 3 readings
  813. * outside threshold for ALS + PXS
  814. */
  815. ret = regmap_write(data->regmap, APDS9960_REG_PERS,
  816. APDS9960_DEFAULT_PERS);
  817. if (ret)
  818. return ret;
  819. /*
  820. * Wait for 4 event outside gesture threshold to prevent interrupt
  821. * flooding.
  822. */
  823. ret = regmap_update_bits(data->regmap, APDS9960_REG_GCONF_1,
  824. APDS9960_REG_GCONF_1_GFIFO_THRES_MASK,
  825. BIT(0) << APDS9960_REG_GCONF_1_GFIFO_THRES_MASK_SHIFT);
  826. if (ret)
  827. return ret;
  828. /* Default ENTER and EXIT thresholds for the GESTURE engine. */
  829. ret = regmap_write(data->regmap, APDS9960_REG_GPENTH,
  830. APDS9960_DEFAULT_GPENTH);
  831. if (ret)
  832. return ret;
  833. ret = regmap_write(data->regmap, APDS9960_REG_GEXTH,
  834. APDS9960_DEFAULT_GEXTH);
  835. if (ret)
  836. return ret;
  837. return apds9960_set_powermode(data, 1);
  838. }
  839. static int apds9960_probe(struct i2c_client *client,
  840. const struct i2c_device_id *id)
  841. {
  842. struct apds9960_data *data;
  843. struct iio_buffer *buffer;
  844. struct iio_dev *indio_dev;
  845. int ret;
  846. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  847. if (!indio_dev)
  848. return -ENOMEM;
  849. buffer = devm_iio_kfifo_allocate(&client->dev);
  850. if (!buffer)
  851. return -ENOMEM;
  852. iio_device_attach_buffer(indio_dev, buffer);
  853. indio_dev->dev.parent = &client->dev;
  854. indio_dev->info = &apds9960_info;
  855. indio_dev->name = APDS9960_DRV_NAME;
  856. indio_dev->channels = apds9960_channels;
  857. indio_dev->num_channels = ARRAY_SIZE(apds9960_channels);
  858. indio_dev->available_scan_masks = apds9960_scan_masks;
  859. indio_dev->modes = (INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE);
  860. indio_dev->setup_ops = &apds9960_buffer_setup_ops;
  861. data = iio_priv(indio_dev);
  862. i2c_set_clientdata(client, indio_dev);
  863. data->regmap = devm_regmap_init_i2c(client, &apds9960_regmap_config);
  864. if (IS_ERR(data->regmap)) {
  865. dev_err(&client->dev, "regmap initialization failed.\n");
  866. return PTR_ERR(data->regmap);
  867. }
  868. data->client = client;
  869. data->indio_dev = indio_dev;
  870. mutex_init(&data->lock);
  871. ret = pm_runtime_set_active(&client->dev);
  872. if (ret)
  873. goto error_power_down;
  874. pm_runtime_enable(&client->dev);
  875. pm_runtime_set_autosuspend_delay(&client->dev, 5000);
  876. pm_runtime_use_autosuspend(&client->dev);
  877. apds9960_set_power_state(data, true);
  878. ret = apds9960_regfield_init(data);
  879. if (ret)
  880. goto error_power_down;
  881. ret = apds9960_chip_init(data);
  882. if (ret)
  883. goto error_power_down;
  884. if (client->irq <= 0) {
  885. dev_err(&client->dev, "no valid irq defined\n");
  886. ret = -EINVAL;
  887. goto error_power_down;
  888. }
  889. ret = devm_request_threaded_irq(&client->dev, client->irq,
  890. NULL, apds9960_interrupt_handler,
  891. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  892. "apds9960_event",
  893. indio_dev);
  894. if (ret) {
  895. dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
  896. goto error_power_down;
  897. }
  898. ret = iio_device_register(indio_dev);
  899. if (ret)
  900. goto error_power_down;
  901. apds9960_set_power_state(data, false);
  902. return 0;
  903. error_power_down:
  904. apds9960_set_power_state(data, false);
  905. return ret;
  906. }
  907. static int apds9960_remove(struct i2c_client *client)
  908. {
  909. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  910. struct apds9960_data *data = iio_priv(indio_dev);
  911. iio_device_unregister(indio_dev);
  912. pm_runtime_disable(&client->dev);
  913. pm_runtime_set_suspended(&client->dev);
  914. apds9960_set_powermode(data, 0);
  915. return 0;
  916. }
  917. #ifdef CONFIG_PM
  918. static int apds9960_runtime_suspend(struct device *dev)
  919. {
  920. struct apds9960_data *data =
  921. iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  922. return apds9960_set_powermode(data, 0);
  923. }
  924. static int apds9960_runtime_resume(struct device *dev)
  925. {
  926. struct apds9960_data *data =
  927. iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
  928. return apds9960_set_powermode(data, 1);
  929. }
  930. #endif
  931. static const struct dev_pm_ops apds9960_pm_ops = {
  932. SET_RUNTIME_PM_OPS(apds9960_runtime_suspend,
  933. apds9960_runtime_resume, NULL)
  934. };
  935. static const struct i2c_device_id apds9960_id[] = {
  936. { "apds9960", 0 },
  937. {}
  938. };
  939. MODULE_DEVICE_TABLE(i2c, apds9960_id);
  940. static struct i2c_driver apds9960_driver = {
  941. .driver = {
  942. .name = APDS9960_DRV_NAME,
  943. .pm = &apds9960_pm_ops,
  944. },
  945. .probe = apds9960_probe,
  946. .remove = apds9960_remove,
  947. .id_table = apds9960_id,
  948. };
  949. module_i2c_driver(apds9960_driver);
  950. MODULE_AUTHOR("Matt Ranostay <mranostay@gmail.com>");
  951. MODULE_DESCRIPTION("ADPS9960 Gesture/RGB/ALS/Proximity sensor");
  952. MODULE_LICENSE("GPL");