bh1770glc.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file is part of the ROHM BH1770GLC / OSRAM SFH7770 sensor driver.
  4. * Chip is combined proximity and ambient light sensor.
  5. *
  6. * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
  7. *
  8. * Contact: Samu Onkalo <samu.p.onkalo@nokia.com>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/i2c.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/mutex.h>
  15. #include <linux/platform_data/bh1770glc.h>
  16. #include <linux/regulator/consumer.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/delay.h>
  20. #include <linux/wait.h>
  21. #include <linux/slab.h>
  22. #define BH1770_ALS_CONTROL 0x80 /* ALS operation mode control */
  23. #define BH1770_PS_CONTROL 0x81 /* PS operation mode control */
  24. #define BH1770_I_LED 0x82 /* active LED and LED1, LED2 current */
  25. #define BH1770_I_LED3 0x83 /* LED3 current setting */
  26. #define BH1770_ALS_PS_MEAS 0x84 /* Forced mode trigger */
  27. #define BH1770_PS_MEAS_RATE 0x85 /* PS meas. rate at stand alone mode */
  28. #define BH1770_ALS_MEAS_RATE 0x86 /* ALS meas. rate at stand alone mode */
  29. #define BH1770_PART_ID 0x8a /* Part number and revision ID */
  30. #define BH1770_MANUFACT_ID 0x8b /* Manufacturerer ID */
  31. #define BH1770_ALS_DATA_0 0x8c /* ALS DATA low byte */
  32. #define BH1770_ALS_DATA_1 0x8d /* ALS DATA high byte */
  33. #define BH1770_ALS_PS_STATUS 0x8e /* Measurement data and int status */
  34. #define BH1770_PS_DATA_LED1 0x8f /* PS data from LED1 */
  35. #define BH1770_PS_DATA_LED2 0x90 /* PS data from LED2 */
  36. #define BH1770_PS_DATA_LED3 0x91 /* PS data from LED3 */
  37. #define BH1770_INTERRUPT 0x92 /* Interrupt setting */
  38. #define BH1770_PS_TH_LED1 0x93 /* PS interrupt threshold for LED1 */
  39. #define BH1770_PS_TH_LED2 0x94 /* PS interrupt threshold for LED2 */
  40. #define BH1770_PS_TH_LED3 0x95 /* PS interrupt threshold for LED3 */
  41. #define BH1770_ALS_TH_UP_0 0x96 /* ALS upper threshold low byte */
  42. #define BH1770_ALS_TH_UP_1 0x97 /* ALS upper threshold high byte */
  43. #define BH1770_ALS_TH_LOW_0 0x98 /* ALS lower threshold low byte */
  44. #define BH1770_ALS_TH_LOW_1 0x99 /* ALS lower threshold high byte */
  45. /* MANUFACT_ID */
  46. #define BH1770_MANUFACT_ROHM 0x01
  47. #define BH1770_MANUFACT_OSRAM 0x03
  48. /* PART_ID */
  49. #define BH1770_PART 0x90
  50. #define BH1770_PART_MASK 0xf0
  51. #define BH1770_REV_MASK 0x0f
  52. #define BH1770_REV_SHIFT 0
  53. #define BH1770_REV_0 0x00
  54. #define BH1770_REV_1 0x01
  55. /* Operating modes for both */
  56. #define BH1770_STANDBY 0x00
  57. #define BH1770_FORCED 0x02
  58. #define BH1770_STANDALONE 0x03
  59. #define BH1770_SWRESET (0x01 << 2)
  60. #define BH1770_PS_TRIG_MEAS (1 << 0)
  61. #define BH1770_ALS_TRIG_MEAS (1 << 1)
  62. /* Interrupt control */
  63. #define BH1770_INT_OUTPUT_MODE (1 << 3) /* 0 = latched */
  64. #define BH1770_INT_POLARITY (1 << 2) /* 1 = active high */
  65. #define BH1770_INT_ALS_ENA (1 << 1)
  66. #define BH1770_INT_PS_ENA (1 << 0)
  67. /* Interrupt status */
  68. #define BH1770_INT_LED1_DATA (1 << 0)
  69. #define BH1770_INT_LED1_INT (1 << 1)
  70. #define BH1770_INT_LED2_DATA (1 << 2)
  71. #define BH1770_INT_LED2_INT (1 << 3)
  72. #define BH1770_INT_LED3_DATA (1 << 4)
  73. #define BH1770_INT_LED3_INT (1 << 5)
  74. #define BH1770_INT_LEDS_INT ((1 << 1) | (1 << 3) | (1 << 5))
  75. #define BH1770_INT_ALS_DATA (1 << 6)
  76. #define BH1770_INT_ALS_INT (1 << 7)
  77. /* Led channels */
  78. #define BH1770_LED1 0x00
  79. #define BH1770_DISABLE 0
  80. #define BH1770_ENABLE 1
  81. #define BH1770_PROX_CHANNELS 1
  82. #define BH1770_LUX_DEFAULT_RATE 1 /* Index to lux rate table */
  83. #define BH1770_PROX_DEFAULT_RATE 1 /* Direct HW value =~ 50Hz */
  84. #define BH1770_PROX_DEF_RATE_THRESH 6 /* Direct HW value =~ 5 Hz */
  85. #define BH1770_STARTUP_DELAY 50
  86. #define BH1770_RESET_TIME 10
  87. #define BH1770_TIMEOUT 2100 /* Timeout in 2.1 seconds */
  88. #define BH1770_LUX_RANGE 65535
  89. #define BH1770_PROX_RANGE 255
  90. #define BH1770_COEF_SCALER 1024
  91. #define BH1770_CALIB_SCALER 8192
  92. #define BH1770_LUX_NEUTRAL_CALIB_VALUE (1 * BH1770_CALIB_SCALER)
  93. #define BH1770_LUX_DEF_THRES 1000
  94. #define BH1770_PROX_DEF_THRES 70
  95. #define BH1770_PROX_DEF_ABS_THRES 100
  96. #define BH1770_DEFAULT_PERSISTENCE 10
  97. #define BH1770_PROX_MAX_PERSISTENCE 50
  98. #define BH1770_LUX_GA_SCALE 16384
  99. #define BH1770_LUX_CF_SCALE 2048 /* CF ChipFactor */
  100. #define BH1770_NEUTRAL_CF BH1770_LUX_CF_SCALE
  101. #define BH1770_LUX_CORR_SCALE 4096
  102. #define PROX_ABOVE_THRESHOLD 1
  103. #define PROX_BELOW_THRESHOLD 0
  104. #define PROX_IGNORE_LUX_LIMIT 500
  105. struct bh1770_chip {
  106. struct bh1770_platform_data *pdata;
  107. char chipname[10];
  108. u8 revision;
  109. struct i2c_client *client;
  110. struct regulator_bulk_data regs[2];
  111. struct mutex mutex; /* avoid parallel access */
  112. wait_queue_head_t wait;
  113. bool int_mode_prox;
  114. bool int_mode_lux;
  115. struct delayed_work prox_work;
  116. u32 lux_cf; /* Chip specific factor */
  117. u32 lux_ga;
  118. u32 lux_calib;
  119. int lux_rate_index;
  120. u32 lux_corr;
  121. u16 lux_data_raw;
  122. u16 lux_threshold_hi;
  123. u16 lux_threshold_lo;
  124. u16 lux_thres_hi_onchip;
  125. u16 lux_thres_lo_onchip;
  126. bool lux_wait_result;
  127. int prox_enable_count;
  128. u16 prox_coef;
  129. u16 prox_const;
  130. int prox_rate;
  131. int prox_rate_threshold;
  132. u8 prox_persistence;
  133. u8 prox_persistence_counter;
  134. u8 prox_data;
  135. u8 prox_threshold;
  136. u8 prox_threshold_hw;
  137. bool prox_force_update;
  138. u8 prox_abs_thres;
  139. u8 prox_led;
  140. };
  141. static const char reg_vcc[] = "Vcc";
  142. static const char reg_vleds[] = "Vleds";
  143. /*
  144. * Supported stand alone rates in ms from chip data sheet
  145. * {10, 20, 30, 40, 70, 100, 200, 500, 1000, 2000};
  146. */
  147. static const s16 prox_rates_hz[] = {100, 50, 33, 25, 14, 10, 5, 2};
  148. static const s16 prox_rates_ms[] = {10, 20, 30, 40, 70, 100, 200, 500};
  149. /*
  150. * Supported stand alone rates in ms from chip data sheet
  151. * {100, 200, 500, 1000, 2000};
  152. */
  153. static const s16 lux_rates_hz[] = {10, 5, 2, 1, 0};
  154. /*
  155. * interrupt control functions are called while keeping chip->mutex
  156. * excluding module probe / remove
  157. */
  158. static inline int bh1770_lux_interrupt_control(struct bh1770_chip *chip,
  159. int lux)
  160. {
  161. chip->int_mode_lux = lux;
  162. /* Set interrupt modes, interrupt active low, latched */
  163. return i2c_smbus_write_byte_data(chip->client,
  164. BH1770_INTERRUPT,
  165. (lux << 1) | chip->int_mode_prox);
  166. }
  167. static inline int bh1770_prox_interrupt_control(struct bh1770_chip *chip,
  168. int ps)
  169. {
  170. chip->int_mode_prox = ps;
  171. return i2c_smbus_write_byte_data(chip->client,
  172. BH1770_INTERRUPT,
  173. (chip->int_mode_lux << 1) | (ps << 0));
  174. }
  175. /* chip->mutex is always kept here */
  176. static int bh1770_lux_rate(struct bh1770_chip *chip, int rate_index)
  177. {
  178. /* sysfs may call this when the chip is powered off */
  179. if (pm_runtime_suspended(&chip->client->dev))
  180. return 0;
  181. /* Proper proximity response needs fastest lux rate (100ms) */
  182. if (chip->prox_enable_count)
  183. rate_index = 0;
  184. return i2c_smbus_write_byte_data(chip->client,
  185. BH1770_ALS_MEAS_RATE,
  186. rate_index);
  187. }
  188. static int bh1770_prox_rate(struct bh1770_chip *chip, int mode)
  189. {
  190. int rate;
  191. rate = (mode == PROX_ABOVE_THRESHOLD) ?
  192. chip->prox_rate_threshold : chip->prox_rate;
  193. return i2c_smbus_write_byte_data(chip->client,
  194. BH1770_PS_MEAS_RATE,
  195. rate);
  196. }
  197. /* InfraredLED is controlled by the chip during proximity scanning */
  198. static inline int bh1770_led_cfg(struct bh1770_chip *chip)
  199. {
  200. /* LED cfg, current for leds 1 and 2 */
  201. return i2c_smbus_write_byte_data(chip->client,
  202. BH1770_I_LED,
  203. (BH1770_LED1 << 6) |
  204. (BH1770_LED_5mA << 3) |
  205. chip->prox_led);
  206. }
  207. /*
  208. * Following two functions converts raw ps values from HW to normalized
  209. * values. Purpose is to compensate differences between different sensor
  210. * versions and variants so that result means about the same between
  211. * versions.
  212. */
  213. static inline u8 bh1770_psraw_to_adjusted(struct bh1770_chip *chip, u8 psraw)
  214. {
  215. u16 adjusted;
  216. adjusted = (u16)(((u32)(psraw + chip->prox_const) * chip->prox_coef) /
  217. BH1770_COEF_SCALER);
  218. if (adjusted > BH1770_PROX_RANGE)
  219. adjusted = BH1770_PROX_RANGE;
  220. return adjusted;
  221. }
  222. static inline u8 bh1770_psadjusted_to_raw(struct bh1770_chip *chip, u8 ps)
  223. {
  224. u16 raw;
  225. raw = (((u32)ps * BH1770_COEF_SCALER) / chip->prox_coef);
  226. if (raw > chip->prox_const)
  227. raw = raw - chip->prox_const;
  228. else
  229. raw = 0;
  230. return raw;
  231. }
  232. /*
  233. * Following two functions converts raw lux values from HW to normalized
  234. * values. Purpose is to compensate differences between different sensor
  235. * versions and variants so that result means about the same between
  236. * versions. Chip->mutex is kept when this is called.
  237. */
  238. static int bh1770_prox_set_threshold(struct bh1770_chip *chip)
  239. {
  240. u8 tmp = 0;
  241. /* sysfs may call this when the chip is powered off */
  242. if (pm_runtime_suspended(&chip->client->dev))
  243. return 0;
  244. tmp = bh1770_psadjusted_to_raw(chip, chip->prox_threshold);
  245. chip->prox_threshold_hw = tmp;
  246. return i2c_smbus_write_byte_data(chip->client, BH1770_PS_TH_LED1,
  247. tmp);
  248. }
  249. static inline u16 bh1770_lux_raw_to_adjusted(struct bh1770_chip *chip, u16 raw)
  250. {
  251. u32 lux;
  252. lux = ((u32)raw * chip->lux_corr) / BH1770_LUX_CORR_SCALE;
  253. return min(lux, (u32)BH1770_LUX_RANGE);
  254. }
  255. static inline u16 bh1770_lux_adjusted_to_raw(struct bh1770_chip *chip,
  256. u16 adjusted)
  257. {
  258. return (u32)adjusted * BH1770_LUX_CORR_SCALE / chip->lux_corr;
  259. }
  260. /* chip->mutex is kept when this is called */
  261. static int bh1770_lux_update_thresholds(struct bh1770_chip *chip,
  262. u16 threshold_hi, u16 threshold_lo)
  263. {
  264. u8 data[4];
  265. int ret;
  266. /* sysfs may call this when the chip is powered off */
  267. if (pm_runtime_suspended(&chip->client->dev))
  268. return 0;
  269. /*
  270. * Compensate threshold values with the correction factors if not
  271. * set to minimum or maximum.
  272. * Min & max values disables interrupts.
  273. */
  274. if (threshold_hi != BH1770_LUX_RANGE && threshold_hi != 0)
  275. threshold_hi = bh1770_lux_adjusted_to_raw(chip, threshold_hi);
  276. if (threshold_lo != BH1770_LUX_RANGE && threshold_lo != 0)
  277. threshold_lo = bh1770_lux_adjusted_to_raw(chip, threshold_lo);
  278. if (chip->lux_thres_hi_onchip == threshold_hi &&
  279. chip->lux_thres_lo_onchip == threshold_lo)
  280. return 0;
  281. chip->lux_thres_hi_onchip = threshold_hi;
  282. chip->lux_thres_lo_onchip = threshold_lo;
  283. data[0] = threshold_hi;
  284. data[1] = threshold_hi >> 8;
  285. data[2] = threshold_lo;
  286. data[3] = threshold_lo >> 8;
  287. ret = i2c_smbus_write_i2c_block_data(chip->client,
  288. BH1770_ALS_TH_UP_0,
  289. ARRAY_SIZE(data),
  290. data);
  291. return ret;
  292. }
  293. static int bh1770_lux_get_result(struct bh1770_chip *chip)
  294. {
  295. u16 data;
  296. int ret;
  297. ret = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_DATA_0);
  298. if (ret < 0)
  299. return ret;
  300. data = ret & 0xff;
  301. ret = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_DATA_1);
  302. if (ret < 0)
  303. return ret;
  304. chip->lux_data_raw = data | ((ret & 0xff) << 8);
  305. return 0;
  306. }
  307. /* Calculate correction value which contains chip and device specific parts */
  308. static u32 bh1770_get_corr_value(struct bh1770_chip *chip)
  309. {
  310. u32 tmp;
  311. /* Impact of glass attenuation correction */
  312. tmp = (BH1770_LUX_CORR_SCALE * chip->lux_ga) / BH1770_LUX_GA_SCALE;
  313. /* Impact of chip factor correction */
  314. tmp = (tmp * chip->lux_cf) / BH1770_LUX_CF_SCALE;
  315. /* Impact of Device specific calibration correction */
  316. tmp = (tmp * chip->lux_calib) / BH1770_CALIB_SCALER;
  317. return tmp;
  318. }
  319. static int bh1770_lux_read_result(struct bh1770_chip *chip)
  320. {
  321. bh1770_lux_get_result(chip);
  322. return bh1770_lux_raw_to_adjusted(chip, chip->lux_data_raw);
  323. }
  324. /*
  325. * Chip on / off functions are called while keeping mutex except probe
  326. * or remove phase
  327. */
  328. static int bh1770_chip_on(struct bh1770_chip *chip)
  329. {
  330. int ret = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
  331. chip->regs);
  332. if (ret < 0)
  333. return ret;
  334. usleep_range(BH1770_STARTUP_DELAY, BH1770_STARTUP_DELAY * 2);
  335. /* Reset the chip */
  336. i2c_smbus_write_byte_data(chip->client, BH1770_ALS_CONTROL,
  337. BH1770_SWRESET);
  338. usleep_range(BH1770_RESET_TIME, BH1770_RESET_TIME * 2);
  339. /*
  340. * ALS is started always since proximity needs als results
  341. * for realibility estimation.
  342. * Let's assume dark until the first ALS measurement is ready.
  343. */
  344. chip->lux_data_raw = 0;
  345. chip->prox_data = 0;
  346. ret = i2c_smbus_write_byte_data(chip->client,
  347. BH1770_ALS_CONTROL, BH1770_STANDALONE);
  348. /* Assume reset defaults */
  349. chip->lux_thres_hi_onchip = BH1770_LUX_RANGE;
  350. chip->lux_thres_lo_onchip = 0;
  351. return ret;
  352. }
  353. static void bh1770_chip_off(struct bh1770_chip *chip)
  354. {
  355. i2c_smbus_write_byte_data(chip->client,
  356. BH1770_INTERRUPT, BH1770_DISABLE);
  357. i2c_smbus_write_byte_data(chip->client,
  358. BH1770_ALS_CONTROL, BH1770_STANDBY);
  359. i2c_smbus_write_byte_data(chip->client,
  360. BH1770_PS_CONTROL, BH1770_STANDBY);
  361. regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
  362. }
  363. /* chip->mutex is kept when this is called */
  364. static int bh1770_prox_mode_control(struct bh1770_chip *chip)
  365. {
  366. if (chip->prox_enable_count) {
  367. chip->prox_force_update = true; /* Force immediate update */
  368. bh1770_lux_rate(chip, chip->lux_rate_index);
  369. bh1770_prox_set_threshold(chip);
  370. bh1770_led_cfg(chip);
  371. bh1770_prox_rate(chip, PROX_BELOW_THRESHOLD);
  372. bh1770_prox_interrupt_control(chip, BH1770_ENABLE);
  373. i2c_smbus_write_byte_data(chip->client,
  374. BH1770_PS_CONTROL, BH1770_STANDALONE);
  375. } else {
  376. chip->prox_data = 0;
  377. bh1770_lux_rate(chip, chip->lux_rate_index);
  378. bh1770_prox_interrupt_control(chip, BH1770_DISABLE);
  379. i2c_smbus_write_byte_data(chip->client,
  380. BH1770_PS_CONTROL, BH1770_STANDBY);
  381. }
  382. return 0;
  383. }
  384. /* chip->mutex is kept when this is called */
  385. static int bh1770_prox_read_result(struct bh1770_chip *chip)
  386. {
  387. int ret;
  388. bool above;
  389. u8 mode;
  390. ret = i2c_smbus_read_byte_data(chip->client, BH1770_PS_DATA_LED1);
  391. if (ret < 0)
  392. goto out;
  393. if (ret > chip->prox_threshold_hw)
  394. above = true;
  395. else
  396. above = false;
  397. /*
  398. * when ALS levels goes above limit, proximity result may be
  399. * false proximity. Thus ignore the result. With real proximity
  400. * there is a shadow causing low als levels.
  401. */
  402. if (chip->lux_data_raw > PROX_IGNORE_LUX_LIMIT)
  403. ret = 0;
  404. chip->prox_data = bh1770_psraw_to_adjusted(chip, ret);
  405. /* Strong proximity level or force mode requires immediate response */
  406. if (chip->prox_data >= chip->prox_abs_thres ||
  407. chip->prox_force_update)
  408. chip->prox_persistence_counter = chip->prox_persistence;
  409. chip->prox_force_update = false;
  410. /* Persistence filttering to reduce false proximity events */
  411. if (likely(above)) {
  412. if (chip->prox_persistence_counter < chip->prox_persistence) {
  413. chip->prox_persistence_counter++;
  414. ret = -ENODATA;
  415. } else {
  416. mode = PROX_ABOVE_THRESHOLD;
  417. ret = 0;
  418. }
  419. } else {
  420. chip->prox_persistence_counter = 0;
  421. mode = PROX_BELOW_THRESHOLD;
  422. chip->prox_data = 0;
  423. ret = 0;
  424. }
  425. /* Set proximity detection rate based on above or below value */
  426. if (ret == 0) {
  427. bh1770_prox_rate(chip, mode);
  428. sysfs_notify(&chip->client->dev.kobj, NULL, "prox0_raw");
  429. }
  430. out:
  431. return ret;
  432. }
  433. static int bh1770_detect(struct bh1770_chip *chip)
  434. {
  435. struct i2c_client *client = chip->client;
  436. s32 ret;
  437. u8 manu, part;
  438. ret = i2c_smbus_read_byte_data(client, BH1770_MANUFACT_ID);
  439. if (ret < 0)
  440. goto error;
  441. manu = (u8)ret;
  442. ret = i2c_smbus_read_byte_data(client, BH1770_PART_ID);
  443. if (ret < 0)
  444. goto error;
  445. part = (u8)ret;
  446. chip->revision = (part & BH1770_REV_MASK) >> BH1770_REV_SHIFT;
  447. chip->prox_coef = BH1770_COEF_SCALER;
  448. chip->prox_const = 0;
  449. chip->lux_cf = BH1770_NEUTRAL_CF;
  450. if ((manu == BH1770_MANUFACT_ROHM) &&
  451. ((part & BH1770_PART_MASK) == BH1770_PART)) {
  452. snprintf(chip->chipname, sizeof(chip->chipname), "BH1770GLC");
  453. return 0;
  454. }
  455. if ((manu == BH1770_MANUFACT_OSRAM) &&
  456. ((part & BH1770_PART_MASK) == BH1770_PART)) {
  457. snprintf(chip->chipname, sizeof(chip->chipname), "SFH7770");
  458. /* Values selected by comparing different versions */
  459. chip->prox_coef = 819; /* 0.8 * BH1770_COEF_SCALER */
  460. chip->prox_const = 40;
  461. return 0;
  462. }
  463. ret = -ENODEV;
  464. error:
  465. dev_dbg(&client->dev, "BH1770 or SFH7770 not found\n");
  466. return ret;
  467. }
  468. /*
  469. * This work is re-scheduled at every proximity interrupt.
  470. * If this work is running, it means that there hasn't been any
  471. * proximity interrupt in time. Situation is handled as no-proximity.
  472. * It would be nice to have low-threshold interrupt or interrupt
  473. * when measurement and hi-threshold are both 0. But neither of those exists.
  474. * This is a workaroud for missing HW feature.
  475. */
  476. static void bh1770_prox_work(struct work_struct *work)
  477. {
  478. struct bh1770_chip *chip =
  479. container_of(work, struct bh1770_chip, prox_work.work);
  480. mutex_lock(&chip->mutex);
  481. bh1770_prox_read_result(chip);
  482. mutex_unlock(&chip->mutex);
  483. }
  484. /* This is threaded irq handler */
  485. static irqreturn_t bh1770_irq(int irq, void *data)
  486. {
  487. struct bh1770_chip *chip = data;
  488. int status;
  489. int rate = 0;
  490. mutex_lock(&chip->mutex);
  491. status = i2c_smbus_read_byte_data(chip->client, BH1770_ALS_PS_STATUS);
  492. /* Acknowledge interrupt by reading this register */
  493. i2c_smbus_read_byte_data(chip->client, BH1770_INTERRUPT);
  494. /*
  495. * Check if there is fresh data available for als.
  496. * If this is the very first data, update thresholds after that.
  497. */
  498. if (status & BH1770_INT_ALS_DATA) {
  499. bh1770_lux_get_result(chip);
  500. if (unlikely(chip->lux_wait_result)) {
  501. chip->lux_wait_result = false;
  502. wake_up(&chip->wait);
  503. bh1770_lux_update_thresholds(chip,
  504. chip->lux_threshold_hi,
  505. chip->lux_threshold_lo);
  506. }
  507. }
  508. /* Disable interrupt logic to guarantee acknowledgement */
  509. i2c_smbus_write_byte_data(chip->client, BH1770_INTERRUPT,
  510. (0 << 1) | (0 << 0));
  511. if ((status & BH1770_INT_ALS_INT))
  512. sysfs_notify(&chip->client->dev.kobj, NULL, "lux0_input");
  513. if (chip->int_mode_prox && (status & BH1770_INT_LEDS_INT)) {
  514. rate = prox_rates_ms[chip->prox_rate_threshold];
  515. bh1770_prox_read_result(chip);
  516. }
  517. /* Re-enable interrupt logic */
  518. i2c_smbus_write_byte_data(chip->client, BH1770_INTERRUPT,
  519. (chip->int_mode_lux << 1) |
  520. (chip->int_mode_prox << 0));
  521. mutex_unlock(&chip->mutex);
  522. /*
  523. * Can't cancel work while keeping mutex since the work uses the
  524. * same mutex.
  525. */
  526. if (rate) {
  527. /*
  528. * Simulate missing no-proximity interrupt 50ms after the
  529. * next expected interrupt time.
  530. */
  531. cancel_delayed_work_sync(&chip->prox_work);
  532. schedule_delayed_work(&chip->prox_work,
  533. msecs_to_jiffies(rate + 50));
  534. }
  535. return IRQ_HANDLED;
  536. }
  537. static ssize_t bh1770_power_state_store(struct device *dev,
  538. struct device_attribute *attr,
  539. const char *buf, size_t count)
  540. {
  541. struct bh1770_chip *chip = dev_get_drvdata(dev);
  542. unsigned long value;
  543. ssize_t ret;
  544. ret = kstrtoul(buf, 0, &value);
  545. if (ret)
  546. return ret;
  547. mutex_lock(&chip->mutex);
  548. if (value) {
  549. pm_runtime_get_sync(dev);
  550. ret = bh1770_lux_rate(chip, chip->lux_rate_index);
  551. if (ret < 0) {
  552. pm_runtime_put(dev);
  553. goto leave;
  554. }
  555. ret = bh1770_lux_interrupt_control(chip, BH1770_ENABLE);
  556. if (ret < 0) {
  557. pm_runtime_put(dev);
  558. goto leave;
  559. }
  560. /* This causes interrupt after the next measurement cycle */
  561. bh1770_lux_update_thresholds(chip, BH1770_LUX_DEF_THRES,
  562. BH1770_LUX_DEF_THRES);
  563. /* Inform that we are waiting for a result from ALS */
  564. chip->lux_wait_result = true;
  565. bh1770_prox_mode_control(chip);
  566. } else if (!pm_runtime_suspended(dev)) {
  567. pm_runtime_put(dev);
  568. }
  569. ret = count;
  570. leave:
  571. mutex_unlock(&chip->mutex);
  572. return ret;
  573. }
  574. static ssize_t bh1770_power_state_show(struct device *dev,
  575. struct device_attribute *attr, char *buf)
  576. {
  577. return sprintf(buf, "%d\n", !pm_runtime_suspended(dev));
  578. }
  579. static ssize_t bh1770_lux_result_show(struct device *dev,
  580. struct device_attribute *attr, char *buf)
  581. {
  582. struct bh1770_chip *chip = dev_get_drvdata(dev);
  583. ssize_t ret;
  584. long timeout;
  585. if (pm_runtime_suspended(dev))
  586. return -EIO; /* Chip is not enabled at all */
  587. timeout = wait_event_interruptible_timeout(chip->wait,
  588. !chip->lux_wait_result,
  589. msecs_to_jiffies(BH1770_TIMEOUT));
  590. if (!timeout)
  591. return -EIO;
  592. mutex_lock(&chip->mutex);
  593. ret = sprintf(buf, "%d\n", bh1770_lux_read_result(chip));
  594. mutex_unlock(&chip->mutex);
  595. return ret;
  596. }
  597. static ssize_t bh1770_lux_range_show(struct device *dev,
  598. struct device_attribute *attr, char *buf)
  599. {
  600. return sprintf(buf, "%d\n", BH1770_LUX_RANGE);
  601. }
  602. static ssize_t bh1770_prox_enable_store(struct device *dev,
  603. struct device_attribute *attr,
  604. const char *buf, size_t count)
  605. {
  606. struct bh1770_chip *chip = dev_get_drvdata(dev);
  607. unsigned long value;
  608. int ret;
  609. ret = kstrtoul(buf, 0, &value);
  610. if (ret)
  611. return ret;
  612. mutex_lock(&chip->mutex);
  613. /* Assume no proximity. Sensor will tell real state soon */
  614. if (!chip->prox_enable_count)
  615. chip->prox_data = 0;
  616. if (value)
  617. chip->prox_enable_count++;
  618. else if (chip->prox_enable_count > 0)
  619. chip->prox_enable_count--;
  620. else
  621. goto leave;
  622. /* Run control only when chip is powered on */
  623. if (!pm_runtime_suspended(dev))
  624. bh1770_prox_mode_control(chip);
  625. leave:
  626. mutex_unlock(&chip->mutex);
  627. return count;
  628. }
  629. static ssize_t bh1770_prox_enable_show(struct device *dev,
  630. struct device_attribute *attr, char *buf)
  631. {
  632. struct bh1770_chip *chip = dev_get_drvdata(dev);
  633. ssize_t len;
  634. mutex_lock(&chip->mutex);
  635. len = sprintf(buf, "%d\n", chip->prox_enable_count);
  636. mutex_unlock(&chip->mutex);
  637. return len;
  638. }
  639. static ssize_t bh1770_prox_result_show(struct device *dev,
  640. struct device_attribute *attr, char *buf)
  641. {
  642. struct bh1770_chip *chip = dev_get_drvdata(dev);
  643. ssize_t ret;
  644. mutex_lock(&chip->mutex);
  645. if (chip->prox_enable_count && !pm_runtime_suspended(dev))
  646. ret = sprintf(buf, "%d\n", chip->prox_data);
  647. else
  648. ret = -EIO;
  649. mutex_unlock(&chip->mutex);
  650. return ret;
  651. }
  652. static ssize_t bh1770_prox_range_show(struct device *dev,
  653. struct device_attribute *attr, char *buf)
  654. {
  655. return sprintf(buf, "%d\n", BH1770_PROX_RANGE);
  656. }
  657. static ssize_t bh1770_get_prox_rate_avail(struct device *dev,
  658. struct device_attribute *attr, char *buf)
  659. {
  660. int i;
  661. int pos = 0;
  662. for (i = 0; i < ARRAY_SIZE(prox_rates_hz); i++)
  663. pos += sprintf(buf + pos, "%d ", prox_rates_hz[i]);
  664. sprintf(buf + pos - 1, "\n");
  665. return pos;
  666. }
  667. static ssize_t bh1770_get_prox_rate_above(struct device *dev,
  668. struct device_attribute *attr, char *buf)
  669. {
  670. struct bh1770_chip *chip = dev_get_drvdata(dev);
  671. return sprintf(buf, "%d\n", prox_rates_hz[chip->prox_rate_threshold]);
  672. }
  673. static ssize_t bh1770_get_prox_rate_below(struct device *dev,
  674. struct device_attribute *attr, char *buf)
  675. {
  676. struct bh1770_chip *chip = dev_get_drvdata(dev);
  677. return sprintf(buf, "%d\n", prox_rates_hz[chip->prox_rate]);
  678. }
  679. static int bh1770_prox_rate_validate(int rate)
  680. {
  681. int i;
  682. for (i = 0; i < ARRAY_SIZE(prox_rates_hz) - 1; i++)
  683. if (rate >= prox_rates_hz[i])
  684. break;
  685. return i;
  686. }
  687. static ssize_t bh1770_set_prox_rate_above(struct device *dev,
  688. struct device_attribute *attr,
  689. const char *buf, size_t count)
  690. {
  691. struct bh1770_chip *chip = dev_get_drvdata(dev);
  692. unsigned long value;
  693. int ret;
  694. ret = kstrtoul(buf, 0, &value);
  695. if (ret)
  696. return ret;
  697. mutex_lock(&chip->mutex);
  698. chip->prox_rate_threshold = bh1770_prox_rate_validate(value);
  699. mutex_unlock(&chip->mutex);
  700. return count;
  701. }
  702. static ssize_t bh1770_set_prox_rate_below(struct device *dev,
  703. struct device_attribute *attr,
  704. const char *buf, size_t count)
  705. {
  706. struct bh1770_chip *chip = dev_get_drvdata(dev);
  707. unsigned long value;
  708. int ret;
  709. ret = kstrtoul(buf, 0, &value);
  710. if (ret)
  711. return ret;
  712. mutex_lock(&chip->mutex);
  713. chip->prox_rate = bh1770_prox_rate_validate(value);
  714. mutex_unlock(&chip->mutex);
  715. return count;
  716. }
  717. static ssize_t bh1770_get_prox_thres(struct device *dev,
  718. struct device_attribute *attr, char *buf)
  719. {
  720. struct bh1770_chip *chip = dev_get_drvdata(dev);
  721. return sprintf(buf, "%d\n", chip->prox_threshold);
  722. }
  723. static ssize_t bh1770_set_prox_thres(struct device *dev,
  724. struct device_attribute *attr,
  725. const char *buf, size_t count)
  726. {
  727. struct bh1770_chip *chip = dev_get_drvdata(dev);
  728. unsigned long value;
  729. int ret;
  730. ret = kstrtoul(buf, 0, &value);
  731. if (ret)
  732. return ret;
  733. if (value > BH1770_PROX_RANGE)
  734. return -EINVAL;
  735. mutex_lock(&chip->mutex);
  736. chip->prox_threshold = value;
  737. ret = bh1770_prox_set_threshold(chip);
  738. mutex_unlock(&chip->mutex);
  739. if (ret < 0)
  740. return ret;
  741. return count;
  742. }
  743. static ssize_t bh1770_prox_persistence_show(struct device *dev,
  744. struct device_attribute *attr, char *buf)
  745. {
  746. struct bh1770_chip *chip = dev_get_drvdata(dev);
  747. return sprintf(buf, "%u\n", chip->prox_persistence);
  748. }
  749. static ssize_t bh1770_prox_persistence_store(struct device *dev,
  750. struct device_attribute *attr,
  751. const char *buf, size_t len)
  752. {
  753. struct bh1770_chip *chip = dev_get_drvdata(dev);
  754. unsigned long value;
  755. int ret;
  756. ret = kstrtoul(buf, 0, &value);
  757. if (ret)
  758. return ret;
  759. if (value > BH1770_PROX_MAX_PERSISTENCE)
  760. return -EINVAL;
  761. chip->prox_persistence = value;
  762. return len;
  763. }
  764. static ssize_t bh1770_prox_abs_thres_show(struct device *dev,
  765. struct device_attribute *attr, char *buf)
  766. {
  767. struct bh1770_chip *chip = dev_get_drvdata(dev);
  768. return sprintf(buf, "%u\n", chip->prox_abs_thres);
  769. }
  770. static ssize_t bh1770_prox_abs_thres_store(struct device *dev,
  771. struct device_attribute *attr,
  772. const char *buf, size_t len)
  773. {
  774. struct bh1770_chip *chip = dev_get_drvdata(dev);
  775. unsigned long value;
  776. int ret;
  777. ret = kstrtoul(buf, 0, &value);
  778. if (ret)
  779. return ret;
  780. if (value > BH1770_PROX_RANGE)
  781. return -EINVAL;
  782. chip->prox_abs_thres = value;
  783. return len;
  784. }
  785. static ssize_t bh1770_chip_id_show(struct device *dev,
  786. struct device_attribute *attr, char *buf)
  787. {
  788. struct bh1770_chip *chip = dev_get_drvdata(dev);
  789. return sprintf(buf, "%s rev %d\n", chip->chipname, chip->revision);
  790. }
  791. static ssize_t bh1770_lux_calib_default_show(struct device *dev,
  792. struct device_attribute *attr, char *buf)
  793. {
  794. return sprintf(buf, "%u\n", BH1770_CALIB_SCALER);
  795. }
  796. static ssize_t bh1770_lux_calib_show(struct device *dev,
  797. struct device_attribute *attr, char *buf)
  798. {
  799. struct bh1770_chip *chip = dev_get_drvdata(dev);
  800. ssize_t len;
  801. mutex_lock(&chip->mutex);
  802. len = sprintf(buf, "%u\n", chip->lux_calib);
  803. mutex_unlock(&chip->mutex);
  804. return len;
  805. }
  806. static ssize_t bh1770_lux_calib_store(struct device *dev,
  807. struct device_attribute *attr,
  808. const char *buf, size_t len)
  809. {
  810. struct bh1770_chip *chip = dev_get_drvdata(dev);
  811. unsigned long value;
  812. u32 old_calib;
  813. u32 new_corr;
  814. int ret;
  815. ret = kstrtoul(buf, 0, &value);
  816. if (ret)
  817. return ret;
  818. mutex_lock(&chip->mutex);
  819. old_calib = chip->lux_calib;
  820. chip->lux_calib = value;
  821. new_corr = bh1770_get_corr_value(chip);
  822. if (new_corr == 0) {
  823. chip->lux_calib = old_calib;
  824. mutex_unlock(&chip->mutex);
  825. return -EINVAL;
  826. }
  827. chip->lux_corr = new_corr;
  828. /* Refresh thresholds on HW after changing correction value */
  829. bh1770_lux_update_thresholds(chip, chip->lux_threshold_hi,
  830. chip->lux_threshold_lo);
  831. mutex_unlock(&chip->mutex);
  832. return len;
  833. }
  834. static ssize_t bh1770_get_lux_rate_avail(struct device *dev,
  835. struct device_attribute *attr, char *buf)
  836. {
  837. int i;
  838. int pos = 0;
  839. for (i = 0; i < ARRAY_SIZE(lux_rates_hz); i++)
  840. pos += sprintf(buf + pos, "%d ", lux_rates_hz[i]);
  841. sprintf(buf + pos - 1, "\n");
  842. return pos;
  843. }
  844. static ssize_t bh1770_get_lux_rate(struct device *dev,
  845. struct device_attribute *attr, char *buf)
  846. {
  847. struct bh1770_chip *chip = dev_get_drvdata(dev);
  848. return sprintf(buf, "%d\n", lux_rates_hz[chip->lux_rate_index]);
  849. }
  850. static ssize_t bh1770_set_lux_rate(struct device *dev,
  851. struct device_attribute *attr,
  852. const char *buf, size_t count)
  853. {
  854. struct bh1770_chip *chip = dev_get_drvdata(dev);
  855. unsigned long rate_hz;
  856. int ret, i;
  857. ret = kstrtoul(buf, 0, &rate_hz);
  858. if (ret)
  859. return ret;
  860. for (i = 0; i < ARRAY_SIZE(lux_rates_hz) - 1; i++)
  861. if (rate_hz >= lux_rates_hz[i])
  862. break;
  863. mutex_lock(&chip->mutex);
  864. chip->lux_rate_index = i;
  865. ret = bh1770_lux_rate(chip, i);
  866. mutex_unlock(&chip->mutex);
  867. if (ret < 0)
  868. return ret;
  869. return count;
  870. }
  871. static ssize_t bh1770_get_lux_thresh_above(struct device *dev,
  872. struct device_attribute *attr, char *buf)
  873. {
  874. struct bh1770_chip *chip = dev_get_drvdata(dev);
  875. return sprintf(buf, "%d\n", chip->lux_threshold_hi);
  876. }
  877. static ssize_t bh1770_get_lux_thresh_below(struct device *dev,
  878. struct device_attribute *attr, char *buf)
  879. {
  880. struct bh1770_chip *chip = dev_get_drvdata(dev);
  881. return sprintf(buf, "%d\n", chip->lux_threshold_lo);
  882. }
  883. static ssize_t bh1770_set_lux_thresh(struct bh1770_chip *chip, u16 *target,
  884. const char *buf)
  885. {
  886. unsigned long thresh;
  887. int ret;
  888. ret = kstrtoul(buf, 0, &thresh);
  889. if (ret)
  890. return ret;
  891. if (thresh > BH1770_LUX_RANGE)
  892. return -EINVAL;
  893. mutex_lock(&chip->mutex);
  894. *target = thresh;
  895. /*
  896. * Don't update values in HW if we are still waiting for
  897. * first interrupt to come after device handle open call.
  898. */
  899. if (!chip->lux_wait_result)
  900. ret = bh1770_lux_update_thresholds(chip,
  901. chip->lux_threshold_hi,
  902. chip->lux_threshold_lo);
  903. mutex_unlock(&chip->mutex);
  904. return ret;
  905. }
  906. static ssize_t bh1770_set_lux_thresh_above(struct device *dev,
  907. struct device_attribute *attr,
  908. const char *buf, size_t len)
  909. {
  910. struct bh1770_chip *chip = dev_get_drvdata(dev);
  911. int ret = bh1770_set_lux_thresh(chip, &chip->lux_threshold_hi, buf);
  912. if (ret < 0)
  913. return ret;
  914. return len;
  915. }
  916. static ssize_t bh1770_set_lux_thresh_below(struct device *dev,
  917. struct device_attribute *attr,
  918. const char *buf, size_t len)
  919. {
  920. struct bh1770_chip *chip = dev_get_drvdata(dev);
  921. int ret = bh1770_set_lux_thresh(chip, &chip->lux_threshold_lo, buf);
  922. if (ret < 0)
  923. return ret;
  924. return len;
  925. }
  926. static DEVICE_ATTR(prox0_raw_en, S_IRUGO | S_IWUSR, bh1770_prox_enable_show,
  927. bh1770_prox_enable_store);
  928. static DEVICE_ATTR(prox0_thresh_above1_value, S_IRUGO | S_IWUSR,
  929. bh1770_prox_abs_thres_show,
  930. bh1770_prox_abs_thres_store);
  931. static DEVICE_ATTR(prox0_thresh_above0_value, S_IRUGO | S_IWUSR,
  932. bh1770_get_prox_thres,
  933. bh1770_set_prox_thres);
  934. static DEVICE_ATTR(prox0_raw, S_IRUGO, bh1770_prox_result_show, NULL);
  935. static DEVICE_ATTR(prox0_sensor_range, S_IRUGO, bh1770_prox_range_show, NULL);
  936. static DEVICE_ATTR(prox0_thresh_above_count, S_IRUGO | S_IWUSR,
  937. bh1770_prox_persistence_show,
  938. bh1770_prox_persistence_store);
  939. static DEVICE_ATTR(prox0_rate_above, S_IRUGO | S_IWUSR,
  940. bh1770_get_prox_rate_above,
  941. bh1770_set_prox_rate_above);
  942. static DEVICE_ATTR(prox0_rate_below, S_IRUGO | S_IWUSR,
  943. bh1770_get_prox_rate_below,
  944. bh1770_set_prox_rate_below);
  945. static DEVICE_ATTR(prox0_rate_avail, S_IRUGO, bh1770_get_prox_rate_avail, NULL);
  946. static DEVICE_ATTR(lux0_calibscale, S_IRUGO | S_IWUSR, bh1770_lux_calib_show,
  947. bh1770_lux_calib_store);
  948. static DEVICE_ATTR(lux0_calibscale_default, S_IRUGO,
  949. bh1770_lux_calib_default_show,
  950. NULL);
  951. static DEVICE_ATTR(lux0_input, S_IRUGO, bh1770_lux_result_show, NULL);
  952. static DEVICE_ATTR(lux0_sensor_range, S_IRUGO, bh1770_lux_range_show, NULL);
  953. static DEVICE_ATTR(lux0_rate, S_IRUGO | S_IWUSR, bh1770_get_lux_rate,
  954. bh1770_set_lux_rate);
  955. static DEVICE_ATTR(lux0_rate_avail, S_IRUGO, bh1770_get_lux_rate_avail, NULL);
  956. static DEVICE_ATTR(lux0_thresh_above_value, S_IRUGO | S_IWUSR,
  957. bh1770_get_lux_thresh_above,
  958. bh1770_set_lux_thresh_above);
  959. static DEVICE_ATTR(lux0_thresh_below_value, S_IRUGO | S_IWUSR,
  960. bh1770_get_lux_thresh_below,
  961. bh1770_set_lux_thresh_below);
  962. static DEVICE_ATTR(chip_id, S_IRUGO, bh1770_chip_id_show, NULL);
  963. static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR, bh1770_power_state_show,
  964. bh1770_power_state_store);
  965. static struct attribute *sysfs_attrs[] = {
  966. &dev_attr_lux0_calibscale.attr,
  967. &dev_attr_lux0_calibscale_default.attr,
  968. &dev_attr_lux0_input.attr,
  969. &dev_attr_lux0_sensor_range.attr,
  970. &dev_attr_lux0_rate.attr,
  971. &dev_attr_lux0_rate_avail.attr,
  972. &dev_attr_lux0_thresh_above_value.attr,
  973. &dev_attr_lux0_thresh_below_value.attr,
  974. &dev_attr_prox0_raw.attr,
  975. &dev_attr_prox0_sensor_range.attr,
  976. &dev_attr_prox0_raw_en.attr,
  977. &dev_attr_prox0_thresh_above_count.attr,
  978. &dev_attr_prox0_rate_above.attr,
  979. &dev_attr_prox0_rate_below.attr,
  980. &dev_attr_prox0_rate_avail.attr,
  981. &dev_attr_prox0_thresh_above0_value.attr,
  982. &dev_attr_prox0_thresh_above1_value.attr,
  983. &dev_attr_chip_id.attr,
  984. &dev_attr_power_state.attr,
  985. NULL
  986. };
  987. static const struct attribute_group bh1770_attribute_group = {
  988. .attrs = sysfs_attrs
  989. };
  990. static int bh1770_probe(struct i2c_client *client,
  991. const struct i2c_device_id *id)
  992. {
  993. struct bh1770_chip *chip;
  994. int err;
  995. chip = devm_kzalloc(&client->dev, sizeof *chip, GFP_KERNEL);
  996. if (!chip)
  997. return -ENOMEM;
  998. i2c_set_clientdata(client, chip);
  999. chip->client = client;
  1000. mutex_init(&chip->mutex);
  1001. init_waitqueue_head(&chip->wait);
  1002. INIT_DELAYED_WORK(&chip->prox_work, bh1770_prox_work);
  1003. if (client->dev.platform_data == NULL) {
  1004. dev_err(&client->dev, "platform data is mandatory\n");
  1005. return -EINVAL;
  1006. }
  1007. chip->pdata = client->dev.platform_data;
  1008. chip->lux_calib = BH1770_LUX_NEUTRAL_CALIB_VALUE;
  1009. chip->lux_rate_index = BH1770_LUX_DEFAULT_RATE;
  1010. chip->lux_threshold_lo = BH1770_LUX_DEF_THRES;
  1011. chip->lux_threshold_hi = BH1770_LUX_DEF_THRES;
  1012. if (chip->pdata->glass_attenuation == 0)
  1013. chip->lux_ga = BH1770_NEUTRAL_GA;
  1014. else
  1015. chip->lux_ga = chip->pdata->glass_attenuation;
  1016. chip->prox_threshold = BH1770_PROX_DEF_THRES;
  1017. chip->prox_led = chip->pdata->led_def_curr;
  1018. chip->prox_abs_thres = BH1770_PROX_DEF_ABS_THRES;
  1019. chip->prox_persistence = BH1770_DEFAULT_PERSISTENCE;
  1020. chip->prox_rate_threshold = BH1770_PROX_DEF_RATE_THRESH;
  1021. chip->prox_rate = BH1770_PROX_DEFAULT_RATE;
  1022. chip->prox_data = 0;
  1023. chip->regs[0].supply = reg_vcc;
  1024. chip->regs[1].supply = reg_vleds;
  1025. err = devm_regulator_bulk_get(&client->dev,
  1026. ARRAY_SIZE(chip->regs), chip->regs);
  1027. if (err < 0) {
  1028. dev_err(&client->dev, "Cannot get regulators\n");
  1029. return err;
  1030. }
  1031. err = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
  1032. chip->regs);
  1033. if (err < 0) {
  1034. dev_err(&client->dev, "Cannot enable regulators\n");
  1035. return err;
  1036. }
  1037. usleep_range(BH1770_STARTUP_DELAY, BH1770_STARTUP_DELAY * 2);
  1038. err = bh1770_detect(chip);
  1039. if (err < 0)
  1040. goto fail0;
  1041. /* Start chip */
  1042. bh1770_chip_on(chip);
  1043. pm_runtime_set_active(&client->dev);
  1044. pm_runtime_enable(&client->dev);
  1045. chip->lux_corr = bh1770_get_corr_value(chip);
  1046. if (chip->lux_corr == 0) {
  1047. dev_err(&client->dev, "Improper correction values\n");
  1048. err = -EINVAL;
  1049. goto fail0;
  1050. }
  1051. if (chip->pdata->setup_resources) {
  1052. err = chip->pdata->setup_resources();
  1053. if (err) {
  1054. err = -EINVAL;
  1055. goto fail0;
  1056. }
  1057. }
  1058. err = sysfs_create_group(&chip->client->dev.kobj,
  1059. &bh1770_attribute_group);
  1060. if (err < 0) {
  1061. dev_err(&chip->client->dev, "Sysfs registration failed\n");
  1062. goto fail1;
  1063. }
  1064. /*
  1065. * Chip needs level triggered interrupt to work. However,
  1066. * level triggering doesn't work always correctly with power
  1067. * management. Select both
  1068. */
  1069. err = request_threaded_irq(client->irq, NULL,
  1070. bh1770_irq,
  1071. IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
  1072. IRQF_TRIGGER_LOW,
  1073. "bh1770", chip);
  1074. if (err) {
  1075. dev_err(&client->dev, "could not get IRQ %d\n",
  1076. client->irq);
  1077. goto fail2;
  1078. }
  1079. regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
  1080. return err;
  1081. fail2:
  1082. sysfs_remove_group(&chip->client->dev.kobj,
  1083. &bh1770_attribute_group);
  1084. fail1:
  1085. if (chip->pdata->release_resources)
  1086. chip->pdata->release_resources();
  1087. fail0:
  1088. regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
  1089. return err;
  1090. }
  1091. static int bh1770_remove(struct i2c_client *client)
  1092. {
  1093. struct bh1770_chip *chip = i2c_get_clientdata(client);
  1094. free_irq(client->irq, chip);
  1095. sysfs_remove_group(&chip->client->dev.kobj,
  1096. &bh1770_attribute_group);
  1097. if (chip->pdata->release_resources)
  1098. chip->pdata->release_resources();
  1099. cancel_delayed_work_sync(&chip->prox_work);
  1100. if (!pm_runtime_suspended(&client->dev))
  1101. bh1770_chip_off(chip);
  1102. pm_runtime_disable(&client->dev);
  1103. pm_runtime_set_suspended(&client->dev);
  1104. return 0;
  1105. }
  1106. #ifdef CONFIG_PM_SLEEP
  1107. static int bh1770_suspend(struct device *dev)
  1108. {
  1109. struct i2c_client *client = to_i2c_client(dev);
  1110. struct bh1770_chip *chip = i2c_get_clientdata(client);
  1111. bh1770_chip_off(chip);
  1112. return 0;
  1113. }
  1114. static int bh1770_resume(struct device *dev)
  1115. {
  1116. struct i2c_client *client = to_i2c_client(dev);
  1117. struct bh1770_chip *chip = i2c_get_clientdata(client);
  1118. int ret = 0;
  1119. bh1770_chip_on(chip);
  1120. if (!pm_runtime_suspended(dev)) {
  1121. /*
  1122. * If we were enabled at suspend time, it is expected
  1123. * everything works nice and smoothly
  1124. */
  1125. ret = bh1770_lux_rate(chip, chip->lux_rate_index);
  1126. ret |= bh1770_lux_interrupt_control(chip, BH1770_ENABLE);
  1127. /* This causes interrupt after the next measurement cycle */
  1128. bh1770_lux_update_thresholds(chip, BH1770_LUX_DEF_THRES,
  1129. BH1770_LUX_DEF_THRES);
  1130. /* Inform that we are waiting for a result from ALS */
  1131. chip->lux_wait_result = true;
  1132. bh1770_prox_mode_control(chip);
  1133. }
  1134. return ret;
  1135. }
  1136. #endif
  1137. #ifdef CONFIG_PM
  1138. static int bh1770_runtime_suspend(struct device *dev)
  1139. {
  1140. struct i2c_client *client = to_i2c_client(dev);
  1141. struct bh1770_chip *chip = i2c_get_clientdata(client);
  1142. bh1770_chip_off(chip);
  1143. return 0;
  1144. }
  1145. static int bh1770_runtime_resume(struct device *dev)
  1146. {
  1147. struct i2c_client *client = to_i2c_client(dev);
  1148. struct bh1770_chip *chip = i2c_get_clientdata(client);
  1149. bh1770_chip_on(chip);
  1150. return 0;
  1151. }
  1152. #endif
  1153. static const struct i2c_device_id bh1770_id[] = {
  1154. {"bh1770glc", 0 },
  1155. {"sfh7770", 0 },
  1156. {}
  1157. };
  1158. MODULE_DEVICE_TABLE(i2c, bh1770_id);
  1159. static const struct dev_pm_ops bh1770_pm_ops = {
  1160. SET_SYSTEM_SLEEP_PM_OPS(bh1770_suspend, bh1770_resume)
  1161. SET_RUNTIME_PM_OPS(bh1770_runtime_suspend, bh1770_runtime_resume, NULL)
  1162. };
  1163. static struct i2c_driver bh1770_driver = {
  1164. .driver = {
  1165. .name = "bh1770glc",
  1166. .pm = &bh1770_pm_ops,
  1167. },
  1168. .probe = bh1770_probe,
  1169. .remove = bh1770_remove,
  1170. .id_table = bh1770_id,
  1171. };
  1172. module_i2c_driver(bh1770_driver);
  1173. MODULE_DESCRIPTION("BH1770GLC / SFH7770 combined ALS and proximity sensor");
  1174. MODULE_AUTHOR("Samu Onkalo, Nokia Corporation");
  1175. MODULE_LICENSE("GPL v2");