apds990x.c 34 KB

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