bh1770glc.c 37 KB

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