apds990x.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  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/i2c/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[][9] = {"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. if (sysfs_streq(buf, reporting_modes[0]))
  713. chip->prox_continuous_mode = 0;
  714. else if (sysfs_streq(buf, reporting_modes[1]))
  715. chip->prox_continuous_mode = 1;
  716. else
  717. return -EINVAL;
  718. return len;
  719. }
  720. static DEVICE_ATTR(prox0_reporting_mode, S_IRUGO | S_IWUSR,
  721. apds990x_prox_reporting_mode_show,
  722. apds990x_prox_reporting_mode_store);
  723. static ssize_t apds990x_prox_reporting_avail_show(struct device *dev,
  724. struct device_attribute *attr, char *buf)
  725. {
  726. return sprintf(buf, "%s %s\n", reporting_modes[0], reporting_modes[1]);
  727. }
  728. static DEVICE_ATTR(prox0_reporting_mode_avail, S_IRUGO | S_IWUSR,
  729. apds990x_prox_reporting_avail_show, NULL);
  730. static ssize_t apds990x_lux_thresh_above_show(struct device *dev,
  731. struct device_attribute *attr, char *buf)
  732. {
  733. struct apds990x_chip *chip = dev_get_drvdata(dev);
  734. return sprintf(buf, "%d\n", chip->lux_thres_hi);
  735. }
  736. static ssize_t apds990x_lux_thresh_below_show(struct device *dev,
  737. struct device_attribute *attr, char *buf)
  738. {
  739. struct apds990x_chip *chip = dev_get_drvdata(dev);
  740. return sprintf(buf, "%d\n", chip->lux_thres_lo);
  741. }
  742. static ssize_t apds990x_set_lux_thresh(struct apds990x_chip *chip, u32 *target,
  743. const char *buf)
  744. {
  745. unsigned long thresh;
  746. int ret;
  747. ret = kstrtoul(buf, 0, &thresh);
  748. if (ret)
  749. return ret;
  750. if (thresh > APDS_RANGE)
  751. return -EINVAL;
  752. mutex_lock(&chip->mutex);
  753. *target = thresh;
  754. /*
  755. * Don't update values in HW if we are still waiting for
  756. * first interrupt to come after device handle open call.
  757. */
  758. if (!chip->lux_wait_fresh_res)
  759. apds990x_refresh_athres(chip);
  760. mutex_unlock(&chip->mutex);
  761. return ret;
  762. }
  763. static ssize_t apds990x_lux_thresh_above_store(struct device *dev,
  764. struct device_attribute *attr,
  765. const char *buf, size_t len)
  766. {
  767. struct apds990x_chip *chip = dev_get_drvdata(dev);
  768. int ret = apds990x_set_lux_thresh(chip, &chip->lux_thres_hi, buf);
  769. if (ret < 0)
  770. return ret;
  771. return len;
  772. }
  773. static ssize_t apds990x_lux_thresh_below_store(struct device *dev,
  774. struct device_attribute *attr,
  775. const char *buf, size_t len)
  776. {
  777. struct apds990x_chip *chip = dev_get_drvdata(dev);
  778. int ret = apds990x_set_lux_thresh(chip, &chip->lux_thres_lo, buf);
  779. if (ret < 0)
  780. return ret;
  781. return len;
  782. }
  783. static DEVICE_ATTR(lux0_thresh_above_value, S_IRUGO | S_IWUSR,
  784. apds990x_lux_thresh_above_show,
  785. apds990x_lux_thresh_above_store);
  786. static DEVICE_ATTR(lux0_thresh_below_value, S_IRUGO | S_IWUSR,
  787. apds990x_lux_thresh_below_show,
  788. apds990x_lux_thresh_below_store);
  789. static ssize_t apds990x_prox_threshold_show(struct device *dev,
  790. struct device_attribute *attr, char *buf)
  791. {
  792. struct apds990x_chip *chip = dev_get_drvdata(dev);
  793. return sprintf(buf, "%d\n", chip->prox_thres);
  794. }
  795. static ssize_t apds990x_prox_threshold_store(struct device *dev,
  796. struct device_attribute *attr,
  797. const char *buf, size_t len)
  798. {
  799. struct apds990x_chip *chip = dev_get_drvdata(dev);
  800. unsigned long value;
  801. int ret;
  802. ret = kstrtoul(buf, 0, &value);
  803. if (ret)
  804. return ret;
  805. if ((value > APDS_RANGE) || (value == 0) ||
  806. (value < APDS_PROX_HYSTERESIS))
  807. return -EINVAL;
  808. mutex_lock(&chip->mutex);
  809. chip->prox_thres = value;
  810. apds990x_force_p_refresh(chip);
  811. mutex_unlock(&chip->mutex);
  812. return len;
  813. }
  814. static DEVICE_ATTR(prox0_thresh_above_value, S_IRUGO | S_IWUSR,
  815. apds990x_prox_threshold_show,
  816. apds990x_prox_threshold_store);
  817. static ssize_t apds990x_power_state_show(struct device *dev,
  818. struct device_attribute *attr, char *buf)
  819. {
  820. return sprintf(buf, "%d\n", !pm_runtime_suspended(dev));
  821. return 0;
  822. }
  823. static ssize_t apds990x_power_state_store(struct device *dev,
  824. struct device_attribute *attr,
  825. const char *buf, size_t len)
  826. {
  827. struct apds990x_chip *chip = dev_get_drvdata(dev);
  828. unsigned long value;
  829. int ret;
  830. ret = kstrtoul(buf, 0, &value);
  831. if (ret)
  832. return ret;
  833. if (value) {
  834. pm_runtime_get_sync(dev);
  835. mutex_lock(&chip->mutex);
  836. chip->lux_wait_fresh_res = true;
  837. apds990x_force_a_refresh(chip);
  838. apds990x_force_p_refresh(chip);
  839. mutex_unlock(&chip->mutex);
  840. } else {
  841. if (!pm_runtime_suspended(dev))
  842. pm_runtime_put(dev);
  843. }
  844. return len;
  845. }
  846. static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR,
  847. apds990x_power_state_show,
  848. apds990x_power_state_store);
  849. static ssize_t apds990x_chip_id_show(struct device *dev,
  850. struct device_attribute *attr, char *buf)
  851. {
  852. struct apds990x_chip *chip = dev_get_drvdata(dev);
  853. return sprintf(buf, "%s %d\n", chip->chipname, chip->revision);
  854. }
  855. static DEVICE_ATTR(chip_id, S_IRUGO, apds990x_chip_id_show, NULL);
  856. static struct attribute *sysfs_attrs_ctrl[] = {
  857. &dev_attr_lux0_calibscale.attr,
  858. &dev_attr_lux0_calibscale_default.attr,
  859. &dev_attr_lux0_input.attr,
  860. &dev_attr_lux0_sensor_range.attr,
  861. &dev_attr_lux0_rate.attr,
  862. &dev_attr_lux0_rate_avail.attr,
  863. &dev_attr_lux0_thresh_above_value.attr,
  864. &dev_attr_lux0_thresh_below_value.attr,
  865. &dev_attr_prox0_raw_en.attr,
  866. &dev_attr_prox0_raw.attr,
  867. &dev_attr_prox0_sensor_range.attr,
  868. &dev_attr_prox0_thresh_above_value.attr,
  869. &dev_attr_prox0_reporting_mode.attr,
  870. &dev_attr_prox0_reporting_mode_avail.attr,
  871. &dev_attr_chip_id.attr,
  872. &dev_attr_power_state.attr,
  873. NULL
  874. };
  875. static struct attribute_group apds990x_attribute_group[] = {
  876. {.attrs = sysfs_attrs_ctrl },
  877. };
  878. static int apds990x_probe(struct i2c_client *client,
  879. const struct i2c_device_id *id)
  880. {
  881. struct apds990x_chip *chip;
  882. int err;
  883. chip = kzalloc(sizeof *chip, GFP_KERNEL);
  884. if (!chip)
  885. return -ENOMEM;
  886. i2c_set_clientdata(client, chip);
  887. chip->client = client;
  888. init_waitqueue_head(&chip->wait);
  889. mutex_init(&chip->mutex);
  890. chip->pdata = client->dev.platform_data;
  891. if (chip->pdata == NULL) {
  892. dev_err(&client->dev, "platform data is mandatory\n");
  893. err = -EINVAL;
  894. goto fail1;
  895. }
  896. if (chip->pdata->cf.ga == 0) {
  897. /* set uncovered sensor default parameters */
  898. chip->cf.ga = 1966; /* 0.48 * APDS_PARAM_SCALE */
  899. chip->cf.cf1 = 4096; /* 1.00 * APDS_PARAM_SCALE */
  900. chip->cf.irf1 = 9134; /* 2.23 * APDS_PARAM_SCALE */
  901. chip->cf.cf2 = 2867; /* 0.70 * APDS_PARAM_SCALE */
  902. chip->cf.irf2 = 5816; /* 1.42 * APDS_PARAM_SCALE */
  903. chip->cf.df = 52;
  904. } else {
  905. chip->cf = chip->pdata->cf;
  906. }
  907. /* precalculate inverse chip factors for threshold control */
  908. chip->rcf.afactor =
  909. (chip->cf.irf1 - chip->cf.irf2) * APDS_PARAM_SCALE /
  910. (chip->cf.cf1 - chip->cf.cf2);
  911. chip->rcf.cf1 = APDS_PARAM_SCALE * APDS_PARAM_SCALE /
  912. chip->cf.cf1;
  913. chip->rcf.irf1 = chip->cf.irf1 * APDS_PARAM_SCALE /
  914. chip->cf.cf1;
  915. chip->rcf.cf2 = APDS_PARAM_SCALE * APDS_PARAM_SCALE /
  916. chip->cf.cf2;
  917. chip->rcf.irf2 = chip->cf.irf2 * APDS_PARAM_SCALE /
  918. chip->cf.cf2;
  919. /* Set something to start with */
  920. chip->lux_thres_hi = APDS_LUX_DEF_THRES_HI;
  921. chip->lux_thres_lo = APDS_LUX_DEF_THRES_LO;
  922. chip->lux_calib = APDS_LUX_NEUTRAL_CALIB_VALUE;
  923. chip->prox_thres = APDS_PROX_DEF_THRES;
  924. chip->pdrive = chip->pdata->pdrive;
  925. chip->pdiode = APDS_PDIODE_IR;
  926. chip->pgain = APDS_PGAIN_1X;
  927. chip->prox_calib = APDS_PROX_NEUTRAL_CALIB_VALUE;
  928. chip->prox_persistence = APDS_DEFAULT_PROX_PERS;
  929. chip->prox_continuous_mode = false;
  930. chip->regs[0].supply = reg_vcc;
  931. chip->regs[1].supply = reg_vled;
  932. err = regulator_bulk_get(&client->dev,
  933. ARRAY_SIZE(chip->regs), chip->regs);
  934. if (err < 0) {
  935. dev_err(&client->dev, "Cannot get regulators\n");
  936. goto fail1;
  937. }
  938. err = regulator_bulk_enable(ARRAY_SIZE(chip->regs), chip->regs);
  939. if (err < 0) {
  940. dev_err(&client->dev, "Cannot enable regulators\n");
  941. goto fail2;
  942. }
  943. usleep_range(APDS_STARTUP_DELAY, 2 * APDS_STARTUP_DELAY);
  944. err = apds990x_detect(chip);
  945. if (err < 0) {
  946. dev_err(&client->dev, "APDS990X not found\n");
  947. goto fail3;
  948. }
  949. pm_runtime_set_active(&client->dev);
  950. apds990x_configure(chip);
  951. apds990x_set_arate(chip, APDS_LUX_DEFAULT_RATE);
  952. apds990x_mode_on(chip);
  953. pm_runtime_enable(&client->dev);
  954. if (chip->pdata->setup_resources) {
  955. err = chip->pdata->setup_resources();
  956. if (err) {
  957. err = -EINVAL;
  958. goto fail3;
  959. }
  960. }
  961. err = sysfs_create_group(&chip->client->dev.kobj,
  962. apds990x_attribute_group);
  963. if (err < 0) {
  964. dev_err(&chip->client->dev, "Sysfs registration failed\n");
  965. goto fail4;
  966. }
  967. err = request_threaded_irq(client->irq, NULL,
  968. apds990x_irq,
  969. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_LOW |
  970. IRQF_ONESHOT,
  971. "apds990x", chip);
  972. if (err) {
  973. dev_err(&client->dev, "could not get IRQ %d\n",
  974. client->irq);
  975. goto fail5;
  976. }
  977. return err;
  978. fail5:
  979. sysfs_remove_group(&chip->client->dev.kobj,
  980. &apds990x_attribute_group[0]);
  981. fail4:
  982. if (chip->pdata && chip->pdata->release_resources)
  983. chip->pdata->release_resources();
  984. fail3:
  985. regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
  986. fail2:
  987. regulator_bulk_free(ARRAY_SIZE(chip->regs), chip->regs);
  988. fail1:
  989. kfree(chip);
  990. return err;
  991. }
  992. static int apds990x_remove(struct i2c_client *client)
  993. {
  994. struct apds990x_chip *chip = i2c_get_clientdata(client);
  995. free_irq(client->irq, chip);
  996. sysfs_remove_group(&chip->client->dev.kobj,
  997. apds990x_attribute_group);
  998. if (chip->pdata && chip->pdata->release_resources)
  999. chip->pdata->release_resources();
  1000. if (!pm_runtime_suspended(&client->dev))
  1001. apds990x_chip_off(chip);
  1002. pm_runtime_disable(&client->dev);
  1003. pm_runtime_set_suspended(&client->dev);
  1004. regulator_bulk_free(ARRAY_SIZE(chip->regs), chip->regs);
  1005. kfree(chip);
  1006. return 0;
  1007. }
  1008. #ifdef CONFIG_PM_SLEEP
  1009. static int apds990x_suspend(struct device *dev)
  1010. {
  1011. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  1012. struct apds990x_chip *chip = i2c_get_clientdata(client);
  1013. apds990x_chip_off(chip);
  1014. return 0;
  1015. }
  1016. static int apds990x_resume(struct device *dev)
  1017. {
  1018. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  1019. struct apds990x_chip *chip = i2c_get_clientdata(client);
  1020. /*
  1021. * If we were enabled at suspend time, it is expected
  1022. * everything works nice and smoothly. Chip_on is enough
  1023. */
  1024. apds990x_chip_on(chip);
  1025. return 0;
  1026. }
  1027. #endif
  1028. #ifdef CONFIG_PM
  1029. static int apds990x_runtime_suspend(struct device *dev)
  1030. {
  1031. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  1032. struct apds990x_chip *chip = i2c_get_clientdata(client);
  1033. apds990x_chip_off(chip);
  1034. return 0;
  1035. }
  1036. static int apds990x_runtime_resume(struct device *dev)
  1037. {
  1038. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  1039. struct apds990x_chip *chip = i2c_get_clientdata(client);
  1040. apds990x_chip_on(chip);
  1041. return 0;
  1042. }
  1043. #endif
  1044. static const struct i2c_device_id apds990x_id[] = {
  1045. {"apds990x", 0 },
  1046. {}
  1047. };
  1048. MODULE_DEVICE_TABLE(i2c, apds990x_id);
  1049. static const struct dev_pm_ops apds990x_pm_ops = {
  1050. SET_SYSTEM_SLEEP_PM_OPS(apds990x_suspend, apds990x_resume)
  1051. SET_RUNTIME_PM_OPS(apds990x_runtime_suspend,
  1052. apds990x_runtime_resume,
  1053. NULL)
  1054. };
  1055. static struct i2c_driver apds990x_driver = {
  1056. .driver = {
  1057. .name = "apds990x",
  1058. .pm = &apds990x_pm_ops,
  1059. },
  1060. .probe = apds990x_probe,
  1061. .remove = apds990x_remove,
  1062. .id_table = apds990x_id,
  1063. };
  1064. module_i2c_driver(apds990x_driver);
  1065. MODULE_DESCRIPTION("APDS990X combined ALS and proximity sensor");
  1066. MODULE_AUTHOR("Samu Onkalo, Nokia Corporation");
  1067. MODULE_LICENSE("GPL v2");