apds9960.c 27 KB

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