apds990x.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file is part of the APDS990x sensor driver.
  4. * Chip is combined proximity and ambient light sensor.
  5. *
  6. * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
  7. *
  8. * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/i2c.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/mutex.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/delay.h>
  18. #include <linux/wait.h>
  19. #include <linux/slab.h>
  20. #include <linux/platform_data/apds990x.h>
  21. /* Register map */
  22. #define APDS990X_ENABLE 0x00 /* Enable of states and interrupts */
  23. #define APDS990X_ATIME 0x01 /* ALS ADC time */
  24. #define APDS990X_PTIME 0x02 /* Proximity ADC time */
  25. #define APDS990X_WTIME 0x03 /* Wait time */
  26. #define APDS990X_AILTL 0x04 /* ALS interrupt low threshold low byte */
  27. #define APDS990X_AILTH 0x05 /* ALS interrupt low threshold hi byte */
  28. #define APDS990X_AIHTL 0x06 /* ALS interrupt hi threshold low byte */
  29. #define APDS990X_AIHTH 0x07 /* ALS interrupt hi threshold hi byte */
  30. #define APDS990X_PILTL 0x08 /* Proximity interrupt low threshold low byte */
  31. #define APDS990X_PILTH 0x09 /* Proximity interrupt low threshold hi byte */
  32. #define APDS990X_PIHTL 0x0a /* Proximity interrupt hi threshold low byte */
  33. #define APDS990X_PIHTH 0x0b /* Proximity interrupt hi threshold hi byte */
  34. #define APDS990X_PERS 0x0c /* Interrupt persistence filters */
  35. #define APDS990X_CONFIG 0x0d /* Configuration */
  36. #define APDS990X_PPCOUNT 0x0e /* Proximity pulse count */
  37. #define APDS990X_CONTROL 0x0f /* Gain control register */
  38. #define APDS990X_REV 0x11 /* Revision Number */
  39. #define APDS990X_ID 0x12 /* Device ID */
  40. #define APDS990X_STATUS 0x13 /* Device status */
  41. #define APDS990X_CDATAL 0x14 /* Clear ADC low data register */
  42. #define APDS990X_CDATAH 0x15 /* Clear ADC high data register */
  43. #define APDS990X_IRDATAL 0x16 /* IR ADC low data register */
  44. #define APDS990X_IRDATAH 0x17 /* IR ADC high data register */
  45. #define APDS990X_PDATAL 0x18 /* Proximity ADC low data register */
  46. #define APDS990X_PDATAH 0x19 /* Proximity ADC high data register */
  47. /* Control */
  48. #define APDS990X_MAX_AGAIN 3
  49. /* Enable register */
  50. #define APDS990X_EN_PIEN (0x1 << 5)
  51. #define APDS990X_EN_AIEN (0x1 << 4)
  52. #define APDS990X_EN_WEN (0x1 << 3)
  53. #define APDS990X_EN_PEN (0x1 << 2)
  54. #define APDS990X_EN_AEN (0x1 << 1)
  55. #define APDS990X_EN_PON (0x1 << 0)
  56. #define APDS990X_EN_DISABLE_ALL 0
  57. /* Status register */
  58. #define APDS990X_ST_PINT (0x1 << 5)
  59. #define APDS990X_ST_AINT (0x1 << 4)
  60. /* I2C access types */
  61. #define APDS990x_CMD_TYPE_MASK (0x03 << 5)
  62. #define APDS990x_CMD_TYPE_RB (0x00 << 5) /* Repeated byte */
  63. #define APDS990x_CMD_TYPE_INC (0x01 << 5) /* Auto increment */
  64. #define APDS990x_CMD_TYPE_SPE (0x03 << 5) /* Special function */
  65. #define APDS990x_ADDR_SHIFT 0
  66. #define APDS990x_CMD 0x80
  67. /* Interrupt ack commands */
  68. #define APDS990X_INT_ACK_ALS 0x6
  69. #define APDS990X_INT_ACK_PS 0x5
  70. #define APDS990X_INT_ACK_BOTH 0x7
  71. /* ptime */
  72. #define APDS990X_PTIME_DEFAULT 0xff /* Recommended conversion time 2.7ms*/
  73. /* wtime */
  74. #define APDS990X_WTIME_DEFAULT 0xee /* ~50ms wait time */
  75. #define APDS990X_TIME_TO_ADC 1024 /* One timetick as ADC count value */
  76. /* Persistence */
  77. #define APDS990X_APERS_SHIFT 0
  78. #define APDS990X_PPERS_SHIFT 4
  79. /* Supported ID:s */
  80. #define APDS990X_ID_0 0x0
  81. #define APDS990X_ID_4 0x4
  82. #define APDS990X_ID_29 0x29
  83. /* pgain and pdiode settings */
  84. #define APDS_PGAIN_1X 0x0
  85. #define APDS_PDIODE_IR 0x2
  86. #define APDS990X_LUX_OUTPUT_SCALE 10
  87. /* Reverse chip factors for threshold calculation */
  88. struct reverse_factors {
  89. u32 afactor;
  90. int cf1;
  91. int irf1;
  92. int cf2;
  93. int irf2;
  94. };
  95. struct apds990x_chip {
  96. struct apds990x_platform_data *pdata;
  97. struct i2c_client *client;
  98. struct mutex mutex; /* avoid parallel access */
  99. struct regulator_bulk_data regs[2];
  100. wait_queue_head_t wait;
  101. int prox_en;
  102. bool prox_continuous_mode;
  103. bool lux_wait_fresh_res;
  104. /* Chip parameters */
  105. struct apds990x_chip_factors cf;
  106. struct reverse_factors rcf;
  107. u16 atime; /* als integration time */
  108. u16 arate; /* als reporting rate */
  109. u16 a_max_result; /* Max possible ADC value with current atime */
  110. u8 again_meas; /* Gain used in last measurement */
  111. u8 again_next; /* Next calculated gain */
  112. u8 pgain;
  113. u8 pdiode;
  114. u8 pdrive;
  115. u8 lux_persistence;
  116. u8 prox_persistence;
  117. u32 lux_raw;
  118. u32 lux;
  119. u16 lux_clear;
  120. u16 lux_ir;
  121. u16 lux_calib;
  122. u32 lux_thres_hi;
  123. u32 lux_thres_lo;
  124. u32 prox_thres;
  125. u16 prox_data;
  126. u16 prox_calib;
  127. char chipname[10];
  128. u8 revision;
  129. };
  130. #define APDS_CALIB_SCALER 8192
  131. #define APDS_LUX_NEUTRAL_CALIB_VALUE (1 * APDS_CALIB_SCALER)
  132. #define APDS_PROX_NEUTRAL_CALIB_VALUE (1 * APDS_CALIB_SCALER)
  133. #define APDS_PROX_DEF_THRES 600
  134. #define APDS_PROX_HYSTERESIS 50
  135. #define APDS_LUX_DEF_THRES_HI 101
  136. #define APDS_LUX_DEF_THRES_LO 100
  137. #define APDS_DEFAULT_PROX_PERS 1
  138. #define APDS_TIMEOUT 2000
  139. #define APDS_STARTUP_DELAY 25000 /* us */
  140. #define APDS_RANGE 65535
  141. #define APDS_PROX_RANGE 1023
  142. #define APDS_LUX_GAIN_LO_LIMIT 100
  143. #define APDS_LUX_GAIN_LO_LIMIT_STRICT 25
  144. #define TIMESTEP 87 /* 2.7ms is about 87 / 32 */
  145. #define TIME_STEP_SCALER 32
  146. #define APDS_LUX_AVERAGING_TIME 50 /* tolerates 50/60Hz ripple */
  147. #define APDS_LUX_DEFAULT_RATE 200
  148. static const u8 again[] = {1, 8, 16, 120}; /* ALS gain steps */
  149. /* Following two tables must match i.e 10Hz rate means 1 as persistence value */
  150. static const u16 arates_hz[] = {10, 5, 2, 1};
  151. static const u8 apersis[] = {1, 2, 4, 5};
  152. /* Regulators */
  153. static const char reg_vcc[] = "Vdd";
  154. static const char reg_vled[] = "Vled";
  155. static int apds990x_read_byte(struct apds990x_chip *chip, u8 reg, u8 *data)
  156. {
  157. struct i2c_client *client = chip->client;
  158. s32 ret;
  159. reg &= ~APDS990x_CMD_TYPE_MASK;
  160. reg |= APDS990x_CMD | APDS990x_CMD_TYPE_RB;
  161. ret = i2c_smbus_read_byte_data(client, reg);
  162. *data = ret;
  163. return (int)ret;
  164. }
  165. static int apds990x_read_word(struct apds990x_chip *chip, u8 reg, u16 *data)
  166. {
  167. struct i2c_client *client = chip->client;
  168. s32 ret;
  169. reg &= ~APDS990x_CMD_TYPE_MASK;
  170. reg |= APDS990x_CMD | APDS990x_CMD_TYPE_INC;
  171. ret = i2c_smbus_read_word_data(client, reg);
  172. *data = ret;
  173. return (int)ret;
  174. }
  175. static int apds990x_write_byte(struct apds990x_chip *chip, u8 reg, u8 data)
  176. {
  177. struct i2c_client *client = chip->client;
  178. s32 ret;
  179. reg &= ~APDS990x_CMD_TYPE_MASK;
  180. reg |= APDS990x_CMD | APDS990x_CMD_TYPE_RB;
  181. ret = i2c_smbus_write_byte_data(client, reg, data);
  182. return (int)ret;
  183. }
  184. static int apds990x_write_word(struct apds990x_chip *chip, u8 reg, u16 data)
  185. {
  186. struct i2c_client *client = chip->client;
  187. s32 ret;
  188. reg &= ~APDS990x_CMD_TYPE_MASK;
  189. reg |= APDS990x_CMD | APDS990x_CMD_TYPE_INC;
  190. ret = i2c_smbus_write_word_data(client, reg, data);
  191. return (int)ret;
  192. }
  193. static int apds990x_mode_on(struct apds990x_chip *chip)
  194. {
  195. /* ALS is mandatory, proximity optional */
  196. u8 reg = APDS990X_EN_AIEN | APDS990X_EN_PON | APDS990X_EN_AEN |
  197. APDS990X_EN_WEN;
  198. if (chip->prox_en)
  199. reg |= APDS990X_EN_PIEN | APDS990X_EN_PEN;
  200. return apds990x_write_byte(chip, APDS990X_ENABLE, reg);
  201. }
  202. static u16 apds990x_lux_to_threshold(struct apds990x_chip *chip, u32 lux)
  203. {
  204. u32 thres;
  205. u32 cpl;
  206. u32 ir;
  207. if (lux == 0)
  208. return 0;
  209. else if (lux == APDS_RANGE)
  210. return APDS_RANGE;
  211. /*
  212. * Reported LUX value is a combination of the IR and CLEAR channel
  213. * values. However, interrupt threshold is only for clear channel.
  214. * This function approximates needed HW threshold value for a given
  215. * LUX value in the current lightning type.
  216. * IR level compared to visible light varies heavily depending on the
  217. * source of the light
  218. *
  219. * Calculate threshold value for the next measurement period.
  220. * Math: threshold = lux * cpl where
  221. * cpl = atime * again / (glass_attenuation * device_factor)
  222. * (count-per-lux)
  223. *
  224. * First remove calibration. Division by four is to avoid overflow
  225. */
  226. lux = lux * (APDS_CALIB_SCALER / 4) / (chip->lux_calib / 4);
  227. /* Multiplication by 64 is to increase accuracy */
  228. cpl = ((u32)chip->atime * (u32)again[chip->again_next] *
  229. APDS_PARAM_SCALE * 64) / (chip->cf.ga * chip->cf.df);
  230. thres = lux * cpl / 64;
  231. /*
  232. * Convert IR light from the latest result to match with
  233. * new gain step. This helps to adapt with the current
  234. * source of light.
  235. */
  236. ir = (u32)chip->lux_ir * (u32)again[chip->again_next] /
  237. (u32)again[chip->again_meas];
  238. /*
  239. * Compensate count with IR light impact
  240. * IAC1 > IAC2 (see apds990x_get_lux for formulas)
  241. */
  242. if (chip->lux_clear * APDS_PARAM_SCALE >=
  243. chip->rcf.afactor * chip->lux_ir)
  244. thres = (chip->rcf.cf1 * thres + chip->rcf.irf1 * ir) /
  245. APDS_PARAM_SCALE;
  246. else
  247. thres = (chip->rcf.cf2 * thres + chip->rcf.irf2 * ir) /
  248. APDS_PARAM_SCALE;
  249. if (thres >= chip->a_max_result)
  250. thres = chip->a_max_result - 1;
  251. return thres;
  252. }
  253. static inline int apds990x_set_atime(struct apds990x_chip *chip, u32 time_ms)
  254. {
  255. u8 reg_value;
  256. chip->atime = time_ms;
  257. /* Formula is specified in the data sheet */
  258. reg_value = 256 - ((time_ms * TIME_STEP_SCALER) / TIMESTEP);
  259. /* Calculate max ADC value for given integration time */
  260. chip->a_max_result = (u16)(256 - reg_value) * APDS990X_TIME_TO_ADC;
  261. return apds990x_write_byte(chip, APDS990X_ATIME, reg_value);
  262. }
  263. /* Called always with mutex locked */
  264. static int apds990x_refresh_pthres(struct apds990x_chip *chip, int data)
  265. {
  266. int ret, lo, hi;
  267. /* If the chip is not in use, don't try to access it */
  268. if (pm_runtime_suspended(&chip->client->dev))
  269. return 0;
  270. if (data < chip->prox_thres) {
  271. lo = 0;
  272. hi = chip->prox_thres;
  273. } else {
  274. lo = chip->prox_thres - APDS_PROX_HYSTERESIS;
  275. if (chip->prox_continuous_mode)
  276. hi = chip->prox_thres;
  277. else
  278. hi = APDS_RANGE;
  279. }
  280. ret = apds990x_write_word(chip, APDS990X_PILTL, lo);
  281. ret |= apds990x_write_word(chip, APDS990X_PIHTL, hi);
  282. return ret;
  283. }
  284. /* Called always with mutex locked */
  285. static int apds990x_refresh_athres(struct apds990x_chip *chip)
  286. {
  287. int ret;
  288. /* If the chip is not in use, don't try to access it */
  289. if (pm_runtime_suspended(&chip->client->dev))
  290. return 0;
  291. ret = apds990x_write_word(chip, APDS990X_AILTL,
  292. apds990x_lux_to_threshold(chip, chip->lux_thres_lo));
  293. ret |= apds990x_write_word(chip, APDS990X_AIHTL,
  294. apds990x_lux_to_threshold(chip, chip->lux_thres_hi));
  295. return ret;
  296. }
  297. /* Called always with mutex locked */
  298. static void apds990x_force_a_refresh(struct apds990x_chip *chip)
  299. {
  300. /* This will force ALS interrupt after the next measurement. */
  301. apds990x_write_word(chip, APDS990X_AILTL, APDS_LUX_DEF_THRES_LO);
  302. apds990x_write_word(chip, APDS990X_AIHTL, APDS_LUX_DEF_THRES_HI);
  303. }
  304. /* Called always with mutex locked */
  305. static void apds990x_force_p_refresh(struct apds990x_chip *chip)
  306. {
  307. /* This will force proximity interrupt after the next measurement. */
  308. apds990x_write_word(chip, APDS990X_PILTL, APDS_PROX_DEF_THRES - 1);
  309. apds990x_write_word(chip, APDS990X_PIHTL, APDS_PROX_DEF_THRES);
  310. }
  311. /* Called always with mutex locked */
  312. static int apds990x_calc_again(struct apds990x_chip *chip)
  313. {
  314. int curr_again = chip->again_meas;
  315. int next_again = chip->again_meas;
  316. int ret = 0;
  317. /* Calculate suitable als gain */
  318. if (chip->lux_clear == chip->a_max_result)
  319. next_again -= 2; /* ALS saturated. Decrease gain by 2 steps */
  320. else if (chip->lux_clear > chip->a_max_result / 2)
  321. next_again--;
  322. else if (chip->lux_clear < APDS_LUX_GAIN_LO_LIMIT_STRICT)
  323. next_again += 2; /* Too dark. Increase gain by 2 steps */
  324. else if (chip->lux_clear < APDS_LUX_GAIN_LO_LIMIT)
  325. next_again++;
  326. /* Limit gain to available range */
  327. if (next_again < 0)
  328. next_again = 0;
  329. else if (next_again > APDS990X_MAX_AGAIN)
  330. next_again = APDS990X_MAX_AGAIN;
  331. /* Let's check can we trust the measured result */
  332. if (chip->lux_clear == chip->a_max_result)
  333. /* Result can be totally garbage due to saturation */
  334. ret = -ERANGE;
  335. else if (next_again != curr_again &&
  336. chip->lux_clear < APDS_LUX_GAIN_LO_LIMIT_STRICT)
  337. /*
  338. * Gain is changed and measurement result is very small.
  339. * Result can be totally garbage due to underflow
  340. */
  341. ret = -ERANGE;
  342. chip->again_next = next_again;
  343. apds990x_write_byte(chip, APDS990X_CONTROL,
  344. (chip->pdrive << 6) |
  345. (chip->pdiode << 4) |
  346. (chip->pgain << 2) |
  347. (chip->again_next << 0));
  348. /*
  349. * Error means bad result -> re-measurement is needed. The forced
  350. * refresh uses fastest possible persistence setting to get result
  351. * as soon as possible.
  352. */
  353. if (ret < 0)
  354. apds990x_force_a_refresh(chip);
  355. else
  356. apds990x_refresh_athres(chip);
  357. return ret;
  358. }
  359. /* Called always with mutex locked */
  360. static int apds990x_get_lux(struct apds990x_chip *chip, int clear, int ir)
  361. {
  362. int iac, iac1, iac2; /* IR adjusted counts */
  363. u32 lpc; /* Lux per count */
  364. /* Formulas:
  365. * iac1 = CF1 * CLEAR_CH - IRF1 * IR_CH
  366. * iac2 = CF2 * CLEAR_CH - IRF2 * IR_CH
  367. */
  368. iac1 = (chip->cf.cf1 * clear - chip->cf.irf1 * ir) / APDS_PARAM_SCALE;
  369. iac2 = (chip->cf.cf2 * clear - chip->cf.irf2 * ir) / APDS_PARAM_SCALE;
  370. iac = max(iac1, iac2);
  371. iac = max(iac, 0);
  372. lpc = APDS990X_LUX_OUTPUT_SCALE * (chip->cf.df * chip->cf.ga) /
  373. (u32)(again[chip->again_meas] * (u32)chip->atime);
  374. return (iac * lpc) / APDS_PARAM_SCALE;
  375. }
  376. static int apds990x_ack_int(struct apds990x_chip *chip, u8 mode)
  377. {
  378. struct i2c_client *client = chip->client;
  379. s32 ret;
  380. u8 reg = APDS990x_CMD | APDS990x_CMD_TYPE_SPE;
  381. switch (mode & (APDS990X_ST_AINT | APDS990X_ST_PINT)) {
  382. case APDS990X_ST_AINT:
  383. reg |= APDS990X_INT_ACK_ALS;
  384. break;
  385. case APDS990X_ST_PINT:
  386. reg |= APDS990X_INT_ACK_PS;
  387. break;
  388. default:
  389. reg |= APDS990X_INT_ACK_BOTH;
  390. break;
  391. }
  392. ret = i2c_smbus_read_byte_data(client, reg);
  393. return (int)ret;
  394. }
  395. static irqreturn_t apds990x_irq(int irq, void *data)
  396. {
  397. struct apds990x_chip *chip = data;
  398. u8 status;
  399. apds990x_read_byte(chip, APDS990X_STATUS, &status);
  400. apds990x_ack_int(chip, status);
  401. mutex_lock(&chip->mutex);
  402. if (!pm_runtime_suspended(&chip->client->dev)) {
  403. if (status & APDS990X_ST_AINT) {
  404. apds990x_read_word(chip, APDS990X_CDATAL,
  405. &chip->lux_clear);
  406. apds990x_read_word(chip, APDS990X_IRDATAL,
  407. &chip->lux_ir);
  408. /* Store used gain for calculations */
  409. chip->again_meas = chip->again_next;
  410. chip->lux_raw = apds990x_get_lux(chip,
  411. chip->lux_clear,
  412. chip->lux_ir);
  413. if (apds990x_calc_again(chip) == 0) {
  414. /* Result is valid */
  415. chip->lux = chip->lux_raw;
  416. chip->lux_wait_fresh_res = false;
  417. wake_up(&chip->wait);
  418. sysfs_notify(&chip->client->dev.kobj,
  419. NULL, "lux0_input");
  420. }
  421. }
  422. if ((status & APDS990X_ST_PINT) && chip->prox_en) {
  423. u16 clr_ch;
  424. apds990x_read_word(chip, APDS990X_CDATAL, &clr_ch);
  425. /*
  426. * If ALS channel is saturated at min gain,
  427. * proximity gives false posivite values.
  428. * Just ignore them.
  429. */
  430. if (chip->again_meas == 0 &&
  431. clr_ch == chip->a_max_result)
  432. chip->prox_data = 0;
  433. else
  434. apds990x_read_word(chip,
  435. APDS990X_PDATAL,
  436. &chip->prox_data);
  437. apds990x_refresh_pthres(chip, chip->prox_data);
  438. if (chip->prox_data < chip->prox_thres)
  439. chip->prox_data = 0;
  440. else if (!chip->prox_continuous_mode)
  441. chip->prox_data = APDS_PROX_RANGE;
  442. sysfs_notify(&chip->client->dev.kobj,
  443. NULL, "prox0_raw");
  444. }
  445. }
  446. mutex_unlock(&chip->mutex);
  447. return IRQ_HANDLED;
  448. }
  449. static int apds990x_configure(struct apds990x_chip *chip)
  450. {
  451. /* It is recommended to use disabled mode during these operations */
  452. apds990x_write_byte(chip, APDS990X_ENABLE, APDS990X_EN_DISABLE_ALL);
  453. /* conversion and wait times for different state machince states */
  454. apds990x_write_byte(chip, APDS990X_PTIME, APDS990X_PTIME_DEFAULT);
  455. apds990x_write_byte(chip, APDS990X_WTIME, APDS990X_WTIME_DEFAULT);
  456. apds990x_set_atime(chip, APDS_LUX_AVERAGING_TIME);
  457. apds990x_write_byte(chip, APDS990X_CONFIG, 0);
  458. /* Persistence levels */
  459. apds990x_write_byte(chip, APDS990X_PERS,
  460. (chip->lux_persistence << APDS990X_APERS_SHIFT) |
  461. (chip->prox_persistence << APDS990X_PPERS_SHIFT));
  462. apds990x_write_byte(chip, APDS990X_PPCOUNT, chip->pdata->ppcount);
  463. /* Start with relatively small gain */
  464. chip->again_meas = 1;
  465. chip->again_next = 1;
  466. apds990x_write_byte(chip, APDS990X_CONTROL,
  467. (chip->pdrive << 6) |
  468. (chip->pdiode << 4) |
  469. (chip->pgain << 2) |
  470. (chip->again_next << 0));
  471. return 0;
  472. }
  473. static int apds990x_detect(struct apds990x_chip *chip)
  474. {
  475. struct i2c_client *client = chip->client;
  476. int ret;
  477. u8 id;
  478. ret = apds990x_read_byte(chip, APDS990X_ID, &id);
  479. if (ret < 0) {
  480. dev_err(&client->dev, "ID read failed\n");
  481. return ret;
  482. }
  483. ret = apds990x_read_byte(chip, APDS990X_REV, &chip->revision);
  484. if (ret < 0) {
  485. dev_err(&client->dev, "REV read failed\n");
  486. return ret;
  487. }
  488. switch (id) {
  489. case APDS990X_ID_0:
  490. case APDS990X_ID_4:
  491. case APDS990X_ID_29:
  492. snprintf(chip->chipname, sizeof(chip->chipname), "APDS-990x");
  493. break;
  494. default:
  495. ret = -ENODEV;
  496. break;
  497. }
  498. return ret;
  499. }
  500. #ifdef CONFIG_PM
  501. static int apds990x_chip_on(struct apds990x_chip *chip)
  502. {
  503. int err = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
  504. chip->regs);
  505. if (err < 0)
  506. return err;
  507. usleep_range(APDS_STARTUP_DELAY, 2 * APDS_STARTUP_DELAY);
  508. /* Refresh all configs in case of regulators were off */
  509. chip->prox_data = 0;
  510. apds990x_configure(chip);
  511. apds990x_mode_on(chip);
  512. return 0;
  513. }
  514. #endif
  515. static int apds990x_chip_off(struct apds990x_chip *chip)
  516. {
  517. apds990x_write_byte(chip, APDS990X_ENABLE, APDS990X_EN_DISABLE_ALL);
  518. regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
  519. return 0;
  520. }
  521. static ssize_t apds990x_lux_show(struct device *dev,
  522. struct device_attribute *attr, char *buf)
  523. {
  524. struct apds990x_chip *chip = dev_get_drvdata(dev);
  525. ssize_t ret;
  526. u32 result;
  527. long timeout;
  528. if (pm_runtime_suspended(dev))
  529. return -EIO;
  530. timeout = wait_event_interruptible_timeout(chip->wait,
  531. !chip->lux_wait_fresh_res,
  532. msecs_to_jiffies(APDS_TIMEOUT));
  533. if (!timeout)
  534. return -EIO;
  535. mutex_lock(&chip->mutex);
  536. result = (chip->lux * chip->lux_calib) / APDS_CALIB_SCALER;
  537. if (result > (APDS_RANGE * APDS990X_LUX_OUTPUT_SCALE))
  538. result = APDS_RANGE * APDS990X_LUX_OUTPUT_SCALE;
  539. ret = sprintf(buf, "%d.%d\n",
  540. result / APDS990X_LUX_OUTPUT_SCALE,
  541. result % APDS990X_LUX_OUTPUT_SCALE);
  542. mutex_unlock(&chip->mutex);
  543. return ret;
  544. }
  545. static DEVICE_ATTR(lux0_input, S_IRUGO, apds990x_lux_show, NULL);
  546. static ssize_t apds990x_lux_range_show(struct device *dev,
  547. struct device_attribute *attr, char *buf)
  548. {
  549. return sprintf(buf, "%u\n", APDS_RANGE);
  550. }
  551. static DEVICE_ATTR(lux0_sensor_range, S_IRUGO, apds990x_lux_range_show, NULL);
  552. static ssize_t apds990x_lux_calib_format_show(struct device *dev,
  553. struct device_attribute *attr, char *buf)
  554. {
  555. return sprintf(buf, "%u\n", APDS_CALIB_SCALER);
  556. }
  557. static DEVICE_ATTR(lux0_calibscale_default, S_IRUGO,
  558. apds990x_lux_calib_format_show, NULL);
  559. static ssize_t apds990x_lux_calib_show(struct device *dev,
  560. struct device_attribute *attr, char *buf)
  561. {
  562. struct apds990x_chip *chip = dev_get_drvdata(dev);
  563. return sprintf(buf, "%u\n", chip->lux_calib);
  564. }
  565. static ssize_t apds990x_lux_calib_store(struct device *dev,
  566. struct device_attribute *attr,
  567. const char *buf, size_t len)
  568. {
  569. struct apds990x_chip *chip = dev_get_drvdata(dev);
  570. unsigned long value;
  571. int ret;
  572. ret = kstrtoul(buf, 0, &value);
  573. if (ret)
  574. return ret;
  575. chip->lux_calib = value;
  576. return len;
  577. }
  578. static DEVICE_ATTR(lux0_calibscale, S_IRUGO | S_IWUSR, apds990x_lux_calib_show,
  579. apds990x_lux_calib_store);
  580. static ssize_t apds990x_rate_avail(struct device *dev,
  581. struct device_attribute *attr, char *buf)
  582. {
  583. int i;
  584. int pos = 0;
  585. for (i = 0; i < ARRAY_SIZE(arates_hz); i++)
  586. pos += sprintf(buf + pos, "%d ", arates_hz[i]);
  587. sprintf(buf + pos - 1, "\n");
  588. return pos;
  589. }
  590. static ssize_t apds990x_rate_show(struct device *dev,
  591. struct device_attribute *attr, char *buf)
  592. {
  593. struct apds990x_chip *chip = dev_get_drvdata(dev);
  594. return sprintf(buf, "%d\n", chip->arate);
  595. }
  596. static int apds990x_set_arate(struct apds990x_chip *chip, int rate)
  597. {
  598. int i;
  599. for (i = 0; i < ARRAY_SIZE(arates_hz); i++)
  600. if (rate >= arates_hz[i])
  601. break;
  602. if (i == ARRAY_SIZE(arates_hz))
  603. return -EINVAL;
  604. /* Pick up corresponding persistence value */
  605. chip->lux_persistence = apersis[i];
  606. chip->arate = arates_hz[i];
  607. /* If the chip is not in use, don't try to access it */
  608. if (pm_runtime_suspended(&chip->client->dev))
  609. return 0;
  610. /* Persistence levels */
  611. return apds990x_write_byte(chip, APDS990X_PERS,
  612. (chip->lux_persistence << APDS990X_APERS_SHIFT) |
  613. (chip->prox_persistence << APDS990X_PPERS_SHIFT));
  614. }
  615. static ssize_t apds990x_rate_store(struct device *dev,
  616. struct device_attribute *attr,
  617. const char *buf, size_t len)
  618. {
  619. struct apds990x_chip *chip = dev_get_drvdata(dev);
  620. unsigned long value;
  621. int ret;
  622. ret = kstrtoul(buf, 0, &value);
  623. if (ret)
  624. return ret;
  625. mutex_lock(&chip->mutex);
  626. ret = apds990x_set_arate(chip, value);
  627. mutex_unlock(&chip->mutex);
  628. if (ret < 0)
  629. return ret;
  630. return len;
  631. }
  632. static DEVICE_ATTR(lux0_rate_avail, S_IRUGO, apds990x_rate_avail, NULL);
  633. static DEVICE_ATTR(lux0_rate, S_IRUGO | S_IWUSR, apds990x_rate_show,
  634. apds990x_rate_store);
  635. static ssize_t apds990x_prox_show(struct device *dev,
  636. struct device_attribute *attr, char *buf)
  637. {
  638. ssize_t ret;
  639. struct apds990x_chip *chip = dev_get_drvdata(dev);
  640. if (pm_runtime_suspended(dev) || !chip->prox_en)
  641. return -EIO;
  642. mutex_lock(&chip->mutex);
  643. ret = sprintf(buf, "%d\n", chip->prox_data);
  644. mutex_unlock(&chip->mutex);
  645. return ret;
  646. }
  647. static DEVICE_ATTR(prox0_raw, S_IRUGO, apds990x_prox_show, NULL);
  648. static ssize_t apds990x_prox_range_show(struct device *dev,
  649. struct device_attribute *attr, char *buf)
  650. {
  651. return sprintf(buf, "%u\n", APDS_PROX_RANGE);
  652. }
  653. static DEVICE_ATTR(prox0_sensor_range, S_IRUGO, apds990x_prox_range_show, NULL);
  654. static ssize_t apds990x_prox_enable_show(struct device *dev,
  655. struct device_attribute *attr, char *buf)
  656. {
  657. struct apds990x_chip *chip = dev_get_drvdata(dev);
  658. return sprintf(buf, "%d\n", chip->prox_en);
  659. }
  660. static ssize_t apds990x_prox_enable_store(struct device *dev,
  661. struct device_attribute *attr,
  662. const char *buf, size_t len)
  663. {
  664. struct apds990x_chip *chip = dev_get_drvdata(dev);
  665. unsigned long value;
  666. int ret;
  667. ret = kstrtoul(buf, 0, &value);
  668. if (ret)
  669. return ret;
  670. mutex_lock(&chip->mutex);
  671. if (!chip->prox_en)
  672. chip->prox_data = 0;
  673. if (value)
  674. chip->prox_en++;
  675. else if (chip->prox_en > 0)
  676. chip->prox_en--;
  677. if (!pm_runtime_suspended(dev))
  678. apds990x_mode_on(chip);
  679. mutex_unlock(&chip->mutex);
  680. return len;
  681. }
  682. static DEVICE_ATTR(prox0_raw_en, S_IRUGO | S_IWUSR, apds990x_prox_enable_show,
  683. apds990x_prox_enable_store);
  684. static const char *reporting_modes[] = {"trigger", "periodic"};
  685. static ssize_t apds990x_prox_reporting_mode_show(struct device *dev,
  686. struct device_attribute *attr, char *buf)
  687. {
  688. struct apds990x_chip *chip = dev_get_drvdata(dev);
  689. return sprintf(buf, "%s\n",
  690. reporting_modes[!!chip->prox_continuous_mode]);
  691. }
  692. static ssize_t apds990x_prox_reporting_mode_store(struct device *dev,
  693. struct device_attribute *attr,
  694. const char *buf, size_t len)
  695. {
  696. struct apds990x_chip *chip = dev_get_drvdata(dev);
  697. int ret;
  698. ret = sysfs_match_string(reporting_modes, buf);
  699. if (ret < 0)
  700. return ret;
  701. chip->prox_continuous_mode = ret;
  702. return len;
  703. }
  704. static DEVICE_ATTR(prox0_reporting_mode, S_IRUGO | S_IWUSR,
  705. apds990x_prox_reporting_mode_show,
  706. apds990x_prox_reporting_mode_store);
  707. static ssize_t apds990x_prox_reporting_avail_show(struct device *dev,
  708. struct device_attribute *attr, char *buf)
  709. {
  710. return sprintf(buf, "%s %s\n", reporting_modes[0], reporting_modes[1]);
  711. }
  712. static DEVICE_ATTR(prox0_reporting_mode_avail, S_IRUGO | S_IWUSR,
  713. apds990x_prox_reporting_avail_show, NULL);
  714. static ssize_t apds990x_lux_thresh_above_show(struct device *dev,
  715. struct device_attribute *attr, char *buf)
  716. {
  717. struct apds990x_chip *chip = dev_get_drvdata(dev);
  718. return sprintf(buf, "%d\n", chip->lux_thres_hi);
  719. }
  720. static ssize_t apds990x_lux_thresh_below_show(struct device *dev,
  721. struct device_attribute *attr, char *buf)
  722. {
  723. struct apds990x_chip *chip = dev_get_drvdata(dev);
  724. return sprintf(buf, "%d\n", chip->lux_thres_lo);
  725. }
  726. static ssize_t apds990x_set_lux_thresh(struct apds990x_chip *chip, u32 *target,
  727. const char *buf)
  728. {
  729. unsigned long thresh;
  730. int ret;
  731. ret = kstrtoul(buf, 0, &thresh);
  732. if (ret)
  733. return ret;
  734. if (thresh > APDS_RANGE)
  735. return -EINVAL;
  736. mutex_lock(&chip->mutex);
  737. *target = thresh;
  738. /*
  739. * Don't update values in HW if we are still waiting for
  740. * first interrupt to come after device handle open call.
  741. */
  742. if (!chip->lux_wait_fresh_res)
  743. apds990x_refresh_athres(chip);
  744. mutex_unlock(&chip->mutex);
  745. return ret;
  746. }
  747. static ssize_t apds990x_lux_thresh_above_store(struct device *dev,
  748. struct device_attribute *attr,
  749. const char *buf, size_t len)
  750. {
  751. struct apds990x_chip *chip = dev_get_drvdata(dev);
  752. int ret = apds990x_set_lux_thresh(chip, &chip->lux_thres_hi, buf);
  753. if (ret < 0)
  754. return ret;
  755. return len;
  756. }
  757. static ssize_t apds990x_lux_thresh_below_store(struct device *dev,
  758. struct device_attribute *attr,
  759. const char *buf, size_t len)
  760. {
  761. struct apds990x_chip *chip = dev_get_drvdata(dev);
  762. int ret = apds990x_set_lux_thresh(chip, &chip->lux_thres_lo, buf);
  763. if (ret < 0)
  764. return ret;
  765. return len;
  766. }
  767. static DEVICE_ATTR(lux0_thresh_above_value, S_IRUGO | S_IWUSR,
  768. apds990x_lux_thresh_above_show,
  769. apds990x_lux_thresh_above_store);
  770. static DEVICE_ATTR(lux0_thresh_below_value, S_IRUGO | S_IWUSR,
  771. apds990x_lux_thresh_below_show,
  772. apds990x_lux_thresh_below_store);
  773. static ssize_t apds990x_prox_threshold_show(struct device *dev,
  774. struct device_attribute *attr, char *buf)
  775. {
  776. struct apds990x_chip *chip = dev_get_drvdata(dev);
  777. return sprintf(buf, "%d\n", chip->prox_thres);
  778. }
  779. static ssize_t apds990x_prox_threshold_store(struct device *dev,
  780. struct device_attribute *attr,
  781. const char *buf, size_t len)
  782. {
  783. struct apds990x_chip *chip = dev_get_drvdata(dev);
  784. unsigned long value;
  785. int ret;
  786. ret = kstrtoul(buf, 0, &value);
  787. if (ret)
  788. return ret;
  789. if ((value > APDS_RANGE) || (value == 0) ||
  790. (value < APDS_PROX_HYSTERESIS))
  791. return -EINVAL;
  792. mutex_lock(&chip->mutex);
  793. chip->prox_thres = value;
  794. apds990x_force_p_refresh(chip);
  795. mutex_unlock(&chip->mutex);
  796. return len;
  797. }
  798. static DEVICE_ATTR(prox0_thresh_above_value, S_IRUGO | S_IWUSR,
  799. apds990x_prox_threshold_show,
  800. apds990x_prox_threshold_store);
  801. static ssize_t apds990x_power_state_show(struct device *dev,
  802. struct device_attribute *attr, char *buf)
  803. {
  804. return sprintf(buf, "%d\n", !pm_runtime_suspended(dev));
  805. return 0;
  806. }
  807. static ssize_t apds990x_power_state_store(struct device *dev,
  808. struct device_attribute *attr,
  809. const char *buf, size_t len)
  810. {
  811. struct apds990x_chip *chip = dev_get_drvdata(dev);
  812. unsigned long value;
  813. int ret;
  814. ret = kstrtoul(buf, 0, &value);
  815. if (ret)
  816. return ret;
  817. if (value) {
  818. pm_runtime_get_sync(dev);
  819. mutex_lock(&chip->mutex);
  820. chip->lux_wait_fresh_res = true;
  821. apds990x_force_a_refresh(chip);
  822. apds990x_force_p_refresh(chip);
  823. mutex_unlock(&chip->mutex);
  824. } else {
  825. if (!pm_runtime_suspended(dev))
  826. pm_runtime_put(dev);
  827. }
  828. return len;
  829. }
  830. static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR,
  831. apds990x_power_state_show,
  832. apds990x_power_state_store);
  833. static ssize_t apds990x_chip_id_show(struct device *dev,
  834. struct device_attribute *attr, char *buf)
  835. {
  836. struct apds990x_chip *chip = dev_get_drvdata(dev);
  837. return sprintf(buf, "%s %d\n", chip->chipname, chip->revision);
  838. }
  839. static DEVICE_ATTR(chip_id, S_IRUGO, apds990x_chip_id_show, NULL);
  840. static struct attribute *sysfs_attrs_ctrl[] = {
  841. &dev_attr_lux0_calibscale.attr,
  842. &dev_attr_lux0_calibscale_default.attr,
  843. &dev_attr_lux0_input.attr,
  844. &dev_attr_lux0_sensor_range.attr,
  845. &dev_attr_lux0_rate.attr,
  846. &dev_attr_lux0_rate_avail.attr,
  847. &dev_attr_lux0_thresh_above_value.attr,
  848. &dev_attr_lux0_thresh_below_value.attr,
  849. &dev_attr_prox0_raw_en.attr,
  850. &dev_attr_prox0_raw.attr,
  851. &dev_attr_prox0_sensor_range.attr,
  852. &dev_attr_prox0_thresh_above_value.attr,
  853. &dev_attr_prox0_reporting_mode.attr,
  854. &dev_attr_prox0_reporting_mode_avail.attr,
  855. &dev_attr_chip_id.attr,
  856. &dev_attr_power_state.attr,
  857. NULL
  858. };
  859. static const struct attribute_group apds990x_attribute_group[] = {
  860. {.attrs = sysfs_attrs_ctrl },
  861. };
  862. static int apds990x_probe(struct i2c_client *client,
  863. const struct i2c_device_id *id)
  864. {
  865. struct apds990x_chip *chip;
  866. int err;
  867. chip = kzalloc(sizeof *chip, GFP_KERNEL);
  868. if (!chip)
  869. return -ENOMEM;
  870. i2c_set_clientdata(client, chip);
  871. chip->client = client;
  872. init_waitqueue_head(&chip->wait);
  873. mutex_init(&chip->mutex);
  874. chip->pdata = client->dev.platform_data;
  875. if (chip->pdata == NULL) {
  876. dev_err(&client->dev, "platform data is mandatory\n");
  877. err = -EINVAL;
  878. goto fail1;
  879. }
  880. if (chip->pdata->cf.ga == 0) {
  881. /* set uncovered sensor default parameters */
  882. chip->cf.ga = 1966; /* 0.48 * APDS_PARAM_SCALE */
  883. chip->cf.cf1 = 4096; /* 1.00 * APDS_PARAM_SCALE */
  884. chip->cf.irf1 = 9134; /* 2.23 * APDS_PARAM_SCALE */
  885. chip->cf.cf2 = 2867; /* 0.70 * APDS_PARAM_SCALE */
  886. chip->cf.irf2 = 5816; /* 1.42 * APDS_PARAM_SCALE */
  887. chip->cf.df = 52;
  888. } else {
  889. chip->cf = chip->pdata->cf;
  890. }
  891. /* precalculate inverse chip factors for threshold control */
  892. chip->rcf.afactor =
  893. (chip->cf.irf1 - chip->cf.irf2) * APDS_PARAM_SCALE /
  894. (chip->cf.cf1 - chip->cf.cf2);
  895. chip->rcf.cf1 = APDS_PARAM_SCALE * APDS_PARAM_SCALE /
  896. chip->cf.cf1;
  897. chip->rcf.irf1 = chip->cf.irf1 * APDS_PARAM_SCALE /
  898. chip->cf.cf1;
  899. chip->rcf.cf2 = APDS_PARAM_SCALE * APDS_PARAM_SCALE /
  900. chip->cf.cf2;
  901. chip->rcf.irf2 = chip->cf.irf2 * APDS_PARAM_SCALE /
  902. chip->cf.cf2;
  903. /* Set something to start with */
  904. chip->lux_thres_hi = APDS_LUX_DEF_THRES_HI;
  905. chip->lux_thres_lo = APDS_LUX_DEF_THRES_LO;
  906. chip->lux_calib = APDS_LUX_NEUTRAL_CALIB_VALUE;
  907. chip->prox_thres = APDS_PROX_DEF_THRES;
  908. chip->pdrive = chip->pdata->pdrive;
  909. chip->pdiode = APDS_PDIODE_IR;
  910. chip->pgain = APDS_PGAIN_1X;
  911. chip->prox_calib = APDS_PROX_NEUTRAL_CALIB_VALUE;
  912. chip->prox_persistence = APDS_DEFAULT_PROX_PERS;
  913. chip->prox_continuous_mode = false;
  914. chip->regs[0].supply = reg_vcc;
  915. chip->regs[1].supply = reg_vled;
  916. err = regulator_bulk_get(&client->dev,
  917. ARRAY_SIZE(chip->regs), chip->regs);
  918. if (err < 0) {
  919. dev_err(&client->dev, "Cannot get regulators\n");
  920. goto fail1;
  921. }
  922. err = regulator_bulk_enable(ARRAY_SIZE(chip->regs), chip->regs);
  923. if (err < 0) {
  924. dev_err(&client->dev, "Cannot enable regulators\n");
  925. goto fail2;
  926. }
  927. usleep_range(APDS_STARTUP_DELAY, 2 * APDS_STARTUP_DELAY);
  928. err = apds990x_detect(chip);
  929. if (err < 0) {
  930. dev_err(&client->dev, "APDS990X not found\n");
  931. goto fail3;
  932. }
  933. pm_runtime_set_active(&client->dev);
  934. apds990x_configure(chip);
  935. apds990x_set_arate(chip, APDS_LUX_DEFAULT_RATE);
  936. apds990x_mode_on(chip);
  937. pm_runtime_enable(&client->dev);
  938. if (chip->pdata->setup_resources) {
  939. err = chip->pdata->setup_resources();
  940. if (err) {
  941. err = -EINVAL;
  942. goto fail3;
  943. }
  944. }
  945. err = sysfs_create_group(&chip->client->dev.kobj,
  946. apds990x_attribute_group);
  947. if (err < 0) {
  948. dev_err(&chip->client->dev, "Sysfs registration failed\n");
  949. goto fail4;
  950. }
  951. err = request_threaded_irq(client->irq, NULL,
  952. apds990x_irq,
  953. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_LOW |
  954. IRQF_ONESHOT,
  955. "apds990x", chip);
  956. if (err) {
  957. dev_err(&client->dev, "could not get IRQ %d\n",
  958. client->irq);
  959. goto fail5;
  960. }
  961. return err;
  962. fail5:
  963. sysfs_remove_group(&chip->client->dev.kobj,
  964. &apds990x_attribute_group[0]);
  965. fail4:
  966. if (chip->pdata && chip->pdata->release_resources)
  967. chip->pdata->release_resources();
  968. fail3:
  969. regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
  970. fail2:
  971. regulator_bulk_free(ARRAY_SIZE(chip->regs), chip->regs);
  972. fail1:
  973. kfree(chip);
  974. return err;
  975. }
  976. static int apds990x_remove(struct i2c_client *client)
  977. {
  978. struct apds990x_chip *chip = i2c_get_clientdata(client);
  979. free_irq(client->irq, chip);
  980. sysfs_remove_group(&chip->client->dev.kobj,
  981. apds990x_attribute_group);
  982. if (chip->pdata && chip->pdata->release_resources)
  983. chip->pdata->release_resources();
  984. if (!pm_runtime_suspended(&client->dev))
  985. apds990x_chip_off(chip);
  986. pm_runtime_disable(&client->dev);
  987. pm_runtime_set_suspended(&client->dev);
  988. regulator_bulk_free(ARRAY_SIZE(chip->regs), chip->regs);
  989. kfree(chip);
  990. return 0;
  991. }
  992. #ifdef CONFIG_PM_SLEEP
  993. static int apds990x_suspend(struct device *dev)
  994. {
  995. struct i2c_client *client = to_i2c_client(dev);
  996. struct apds990x_chip *chip = i2c_get_clientdata(client);
  997. apds990x_chip_off(chip);
  998. return 0;
  999. }
  1000. static int apds990x_resume(struct device *dev)
  1001. {
  1002. struct i2c_client *client = to_i2c_client(dev);
  1003. struct apds990x_chip *chip = i2c_get_clientdata(client);
  1004. /*
  1005. * If we were enabled at suspend time, it is expected
  1006. * everything works nice and smoothly. Chip_on is enough
  1007. */
  1008. apds990x_chip_on(chip);
  1009. return 0;
  1010. }
  1011. #endif
  1012. #ifdef CONFIG_PM
  1013. static int apds990x_runtime_suspend(struct device *dev)
  1014. {
  1015. struct i2c_client *client = to_i2c_client(dev);
  1016. struct apds990x_chip *chip = i2c_get_clientdata(client);
  1017. apds990x_chip_off(chip);
  1018. return 0;
  1019. }
  1020. static int apds990x_runtime_resume(struct device *dev)
  1021. {
  1022. struct i2c_client *client = to_i2c_client(dev);
  1023. struct apds990x_chip *chip = i2c_get_clientdata(client);
  1024. apds990x_chip_on(chip);
  1025. return 0;
  1026. }
  1027. #endif
  1028. static const struct i2c_device_id apds990x_id[] = {
  1029. {"apds990x", 0 },
  1030. {}
  1031. };
  1032. MODULE_DEVICE_TABLE(i2c, apds990x_id);
  1033. static const struct dev_pm_ops apds990x_pm_ops = {
  1034. SET_SYSTEM_SLEEP_PM_OPS(apds990x_suspend, apds990x_resume)
  1035. SET_RUNTIME_PM_OPS(apds990x_runtime_suspend,
  1036. apds990x_runtime_resume,
  1037. NULL)
  1038. };
  1039. static struct i2c_driver apds990x_driver = {
  1040. .driver = {
  1041. .name = "apds990x",
  1042. .pm = &apds990x_pm_ops,
  1043. },
  1044. .probe = apds990x_probe,
  1045. .remove = apds990x_remove,
  1046. .id_table = apds990x_id,
  1047. };
  1048. module_i2c_driver(apds990x_driver);
  1049. MODULE_DESCRIPTION("APDS990X combined ALS and proximity sensor");
  1050. MODULE_AUTHOR("Samu Onkalo, Nokia Corporation");
  1051. MODULE_LICENSE("GPL v2");