bh1770glc.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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/i2c/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 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 = container_of(dev, struct 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 = container_of(dev, struct 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 = container_of(dev, struct 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 = container_of(dev, struct 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");