apds990x.c 34 KB

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