bh1770glc.c 37 KB

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