bh1770glc.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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. if (strict_strtoul(buf, 0, &value))
  561. return -EINVAL;
  562. mutex_lock(&chip->mutex);
  563. if (value) {
  564. pm_runtime_get_sync(dev);
  565. ret = bh1770_lux_rate(chip, chip->lux_rate_index);
  566. if (ret < 0) {
  567. pm_runtime_put(dev);
  568. goto leave;
  569. }
  570. ret = bh1770_lux_interrupt_control(chip, BH1770_ENABLE);
  571. if (ret < 0) {
  572. pm_runtime_put(dev);
  573. goto leave;
  574. }
  575. /* This causes interrupt after the next measurement cycle */
  576. bh1770_lux_update_thresholds(chip, BH1770_LUX_DEF_THRES,
  577. BH1770_LUX_DEF_THRES);
  578. /* Inform that we are waiting for a result from ALS */
  579. chip->lux_wait_result = true;
  580. bh1770_prox_mode_control(chip);
  581. } else if (!pm_runtime_suspended(dev)) {
  582. pm_runtime_put(dev);
  583. }
  584. ret = count;
  585. leave:
  586. mutex_unlock(&chip->mutex);
  587. return ret;
  588. }
  589. static ssize_t bh1770_power_state_show(struct device *dev,
  590. struct device_attribute *attr, char *buf)
  591. {
  592. return sprintf(buf, "%d\n", !pm_runtime_suspended(dev));
  593. }
  594. static ssize_t bh1770_lux_result_show(struct device *dev,
  595. struct device_attribute *attr, char *buf)
  596. {
  597. struct bh1770_chip *chip = dev_get_drvdata(dev);
  598. ssize_t ret;
  599. long timeout;
  600. if (pm_runtime_suspended(dev))
  601. return -EIO; /* Chip is not enabled at all */
  602. timeout = wait_event_interruptible_timeout(chip->wait,
  603. !chip->lux_wait_result,
  604. msecs_to_jiffies(BH1770_TIMEOUT));
  605. if (!timeout)
  606. return -EIO;
  607. mutex_lock(&chip->mutex);
  608. ret = sprintf(buf, "%d\n", bh1770_lux_read_result(chip));
  609. mutex_unlock(&chip->mutex);
  610. return ret;
  611. }
  612. static ssize_t bh1770_lux_range_show(struct device *dev,
  613. struct device_attribute *attr, char *buf)
  614. {
  615. return sprintf(buf, "%d\n", BH1770_LUX_RANGE);
  616. }
  617. static ssize_t bh1770_prox_enable_store(struct device *dev,
  618. struct device_attribute *attr,
  619. const char *buf, size_t count)
  620. {
  621. struct bh1770_chip *chip = dev_get_drvdata(dev);
  622. unsigned long value;
  623. if (strict_strtoul(buf, 0, &value))
  624. return -EINVAL;
  625. mutex_lock(&chip->mutex);
  626. /* Assume no proximity. Sensor will tell real state soon */
  627. if (!chip->prox_enable_count)
  628. chip->prox_data = 0;
  629. if (value)
  630. chip->prox_enable_count++;
  631. else if (chip->prox_enable_count > 0)
  632. chip->prox_enable_count--;
  633. else
  634. goto leave;
  635. /* Run control only when chip is powered on */
  636. if (!pm_runtime_suspended(dev))
  637. bh1770_prox_mode_control(chip);
  638. leave:
  639. mutex_unlock(&chip->mutex);
  640. return count;
  641. }
  642. static ssize_t bh1770_prox_enable_show(struct device *dev,
  643. struct device_attribute *attr, char *buf)
  644. {
  645. struct bh1770_chip *chip = dev_get_drvdata(dev);
  646. ssize_t len;
  647. mutex_lock(&chip->mutex);
  648. len = sprintf(buf, "%d\n", chip->prox_enable_count);
  649. mutex_unlock(&chip->mutex);
  650. return len;
  651. }
  652. static ssize_t bh1770_prox_result_show(struct device *dev,
  653. struct device_attribute *attr, char *buf)
  654. {
  655. struct bh1770_chip *chip = dev_get_drvdata(dev);
  656. ssize_t ret;
  657. mutex_lock(&chip->mutex);
  658. if (chip->prox_enable_count && !pm_runtime_suspended(dev))
  659. ret = sprintf(buf, "%d\n", chip->prox_data);
  660. else
  661. ret = -EIO;
  662. mutex_unlock(&chip->mutex);
  663. return ret;
  664. }
  665. static ssize_t bh1770_prox_range_show(struct device *dev,
  666. struct device_attribute *attr, char *buf)
  667. {
  668. return sprintf(buf, "%d\n", BH1770_PROX_RANGE);
  669. }
  670. static ssize_t bh1770_get_prox_rate_avail(struct device *dev,
  671. struct device_attribute *attr, char *buf)
  672. {
  673. int i;
  674. int pos = 0;
  675. for (i = 0; i < ARRAY_SIZE(prox_rates_hz); i++)
  676. pos += sprintf(buf + pos, "%d ", prox_rates_hz[i]);
  677. sprintf(buf + pos - 1, "\n");
  678. return pos;
  679. }
  680. static ssize_t bh1770_get_prox_rate_above(struct device *dev,
  681. struct device_attribute *attr, char *buf)
  682. {
  683. struct bh1770_chip *chip = dev_get_drvdata(dev);
  684. return sprintf(buf, "%d\n", prox_rates_hz[chip->prox_rate_threshold]);
  685. }
  686. static ssize_t bh1770_get_prox_rate_below(struct device *dev,
  687. struct device_attribute *attr, char *buf)
  688. {
  689. struct bh1770_chip *chip = dev_get_drvdata(dev);
  690. return sprintf(buf, "%d\n", prox_rates_hz[chip->prox_rate]);
  691. }
  692. static int bh1770_prox_rate_validate(int rate)
  693. {
  694. int i;
  695. for (i = 0; i < ARRAY_SIZE(prox_rates_hz) - 1; i++)
  696. if (rate >= prox_rates_hz[i])
  697. break;
  698. return i;
  699. }
  700. static ssize_t bh1770_set_prox_rate_above(struct device *dev,
  701. struct device_attribute *attr,
  702. const char *buf, size_t count)
  703. {
  704. struct bh1770_chip *chip = dev_get_drvdata(dev);
  705. unsigned long value;
  706. if (strict_strtoul(buf, 0, &value))
  707. return -EINVAL;
  708. mutex_lock(&chip->mutex);
  709. chip->prox_rate_threshold = bh1770_prox_rate_validate(value);
  710. mutex_unlock(&chip->mutex);
  711. return count;
  712. }
  713. static ssize_t bh1770_set_prox_rate_below(struct device *dev,
  714. struct device_attribute *attr,
  715. const char *buf, size_t count)
  716. {
  717. struct bh1770_chip *chip = dev_get_drvdata(dev);
  718. unsigned long value;
  719. if (strict_strtoul(buf, 0, &value))
  720. return -EINVAL;
  721. mutex_lock(&chip->mutex);
  722. chip->prox_rate = bh1770_prox_rate_validate(value);
  723. mutex_unlock(&chip->mutex);
  724. return count;
  725. }
  726. static ssize_t bh1770_get_prox_thres(struct device *dev,
  727. struct device_attribute *attr, char *buf)
  728. {
  729. struct bh1770_chip *chip = dev_get_drvdata(dev);
  730. return sprintf(buf, "%d\n", chip->prox_threshold);
  731. }
  732. static ssize_t bh1770_set_prox_thres(struct device *dev,
  733. struct device_attribute *attr,
  734. const char *buf, size_t count)
  735. {
  736. struct bh1770_chip *chip = dev_get_drvdata(dev);
  737. unsigned long value;
  738. int ret;
  739. if (strict_strtoul(buf, 0, &value))
  740. return -EINVAL;
  741. if (value > BH1770_PROX_RANGE)
  742. return -EINVAL;
  743. mutex_lock(&chip->mutex);
  744. chip->prox_threshold = value;
  745. ret = bh1770_prox_set_threshold(chip);
  746. mutex_unlock(&chip->mutex);
  747. if (ret < 0)
  748. return ret;
  749. return count;
  750. }
  751. static ssize_t bh1770_prox_persistence_show(struct device *dev,
  752. struct device_attribute *attr, char *buf)
  753. {
  754. struct bh1770_chip *chip = dev_get_drvdata(dev);
  755. return sprintf(buf, "%u\n", chip->prox_persistence);
  756. }
  757. static ssize_t bh1770_prox_persistence_store(struct device *dev,
  758. struct device_attribute *attr,
  759. const char *buf, size_t len)
  760. {
  761. struct bh1770_chip *chip = dev_get_drvdata(dev);
  762. unsigned long value;
  763. if (strict_strtoul(buf, 0, &value))
  764. return -EINVAL;
  765. if (value > BH1770_PROX_MAX_PERSISTENCE)
  766. return -EINVAL;
  767. chip->prox_persistence = value;
  768. return len;
  769. }
  770. static ssize_t bh1770_prox_abs_thres_show(struct device *dev,
  771. struct device_attribute *attr, char *buf)
  772. {
  773. struct bh1770_chip *chip = dev_get_drvdata(dev);
  774. return sprintf(buf, "%u\n", chip->prox_abs_thres);
  775. }
  776. static ssize_t bh1770_prox_abs_thres_store(struct device *dev,
  777. struct device_attribute *attr,
  778. const char *buf, size_t len)
  779. {
  780. struct bh1770_chip *chip = dev_get_drvdata(dev);
  781. unsigned long value;
  782. if (strict_strtoul(buf, 0, &value))
  783. return -EINVAL;
  784. if (value > BH1770_PROX_RANGE)
  785. return -EINVAL;
  786. chip->prox_abs_thres = value;
  787. return len;
  788. }
  789. static ssize_t bh1770_chip_id_show(struct device *dev,
  790. struct device_attribute *attr, char *buf)
  791. {
  792. struct bh1770_chip *chip = dev_get_drvdata(dev);
  793. return sprintf(buf, "%s rev %d\n", chip->chipname, chip->revision);
  794. }
  795. static ssize_t bh1770_lux_calib_default_show(struct device *dev,
  796. struct device_attribute *attr, char *buf)
  797. {
  798. return sprintf(buf, "%u\n", BH1770_CALIB_SCALER);
  799. }
  800. static ssize_t bh1770_lux_calib_show(struct device *dev,
  801. struct device_attribute *attr, char *buf)
  802. {
  803. struct bh1770_chip *chip = dev_get_drvdata(dev);
  804. ssize_t len;
  805. mutex_lock(&chip->mutex);
  806. len = sprintf(buf, "%u\n", chip->lux_calib);
  807. mutex_unlock(&chip->mutex);
  808. return len;
  809. }
  810. static ssize_t bh1770_lux_calib_store(struct device *dev,
  811. struct device_attribute *attr,
  812. const char *buf, size_t len)
  813. {
  814. struct bh1770_chip *chip = dev_get_drvdata(dev);
  815. unsigned long value;
  816. u32 old_calib;
  817. u32 new_corr;
  818. if (strict_strtoul(buf, 0, &value))
  819. return -EINVAL;
  820. mutex_lock(&chip->mutex);
  821. old_calib = chip->lux_calib;
  822. chip->lux_calib = value;
  823. new_corr = bh1770_get_corr_value(chip);
  824. if (new_corr == 0) {
  825. chip->lux_calib = old_calib;
  826. mutex_unlock(&chip->mutex);
  827. return -EINVAL;
  828. }
  829. chip->lux_corr = new_corr;
  830. /* Refresh thresholds on HW after changing correction value */
  831. bh1770_lux_update_thresholds(chip, chip->lux_threshold_hi,
  832. chip->lux_threshold_lo);
  833. mutex_unlock(&chip->mutex);
  834. return len;
  835. }
  836. static ssize_t bh1770_get_lux_rate_avail(struct device *dev,
  837. struct device_attribute *attr, char *buf)
  838. {
  839. int i;
  840. int pos = 0;
  841. for (i = 0; i < ARRAY_SIZE(lux_rates_hz); i++)
  842. pos += sprintf(buf + pos, "%d ", lux_rates_hz[i]);
  843. sprintf(buf + pos - 1, "\n");
  844. return pos;
  845. }
  846. static ssize_t bh1770_get_lux_rate(struct device *dev,
  847. struct device_attribute *attr, char *buf)
  848. {
  849. struct bh1770_chip *chip = dev_get_drvdata(dev);
  850. return sprintf(buf, "%d\n", lux_rates_hz[chip->lux_rate_index]);
  851. }
  852. static ssize_t bh1770_set_lux_rate(struct device *dev,
  853. struct device_attribute *attr,
  854. const char *buf, size_t count)
  855. {
  856. struct bh1770_chip *chip = dev_get_drvdata(dev);
  857. unsigned long rate_hz;
  858. int ret, i;
  859. if (strict_strtoul(buf, 0, &rate_hz))
  860. return -EINVAL;
  861. for (i = 0; i < ARRAY_SIZE(lux_rates_hz) - 1; i++)
  862. if (rate_hz >= lux_rates_hz[i])
  863. break;
  864. mutex_lock(&chip->mutex);
  865. chip->lux_rate_index = i;
  866. ret = bh1770_lux_rate(chip, i);
  867. mutex_unlock(&chip->mutex);
  868. if (ret < 0)
  869. return ret;
  870. return count;
  871. }
  872. static ssize_t bh1770_get_lux_thresh_above(struct device *dev,
  873. struct device_attribute *attr, char *buf)
  874. {
  875. struct bh1770_chip *chip = dev_get_drvdata(dev);
  876. return sprintf(buf, "%d\n", chip->lux_threshold_hi);
  877. }
  878. static ssize_t bh1770_get_lux_thresh_below(struct device *dev,
  879. struct device_attribute *attr, char *buf)
  880. {
  881. struct bh1770_chip *chip = dev_get_drvdata(dev);
  882. return sprintf(buf, "%d\n", chip->lux_threshold_lo);
  883. }
  884. static ssize_t bh1770_set_lux_thresh(struct bh1770_chip *chip, u16 *target,
  885. const char *buf)
  886. {
  887. int ret = 0;
  888. unsigned long thresh;
  889. if (strict_strtoul(buf, 0, &thresh))
  890. return -EINVAL;
  891. if (thresh > BH1770_LUX_RANGE)
  892. return -EINVAL;
  893. mutex_lock(&chip->mutex);
  894. *target = thresh;
  895. /*
  896. * Don't update values in HW if we are still waiting for
  897. * first interrupt to come after device handle open call.
  898. */
  899. if (!chip->lux_wait_result)
  900. ret = bh1770_lux_update_thresholds(chip,
  901. chip->lux_threshold_hi,
  902. chip->lux_threshold_lo);
  903. mutex_unlock(&chip->mutex);
  904. return ret;
  905. }
  906. static ssize_t bh1770_set_lux_thresh_above(struct device *dev,
  907. struct device_attribute *attr,
  908. const char *buf, size_t len)
  909. {
  910. struct bh1770_chip *chip = dev_get_drvdata(dev);
  911. int ret = bh1770_set_lux_thresh(chip, &chip->lux_threshold_hi, buf);
  912. if (ret < 0)
  913. return ret;
  914. return len;
  915. }
  916. static ssize_t bh1770_set_lux_thresh_below(struct device *dev,
  917. struct device_attribute *attr,
  918. const char *buf, size_t len)
  919. {
  920. struct bh1770_chip *chip = dev_get_drvdata(dev);
  921. int ret = bh1770_set_lux_thresh(chip, &chip->lux_threshold_lo, buf);
  922. if (ret < 0)
  923. return ret;
  924. return len;
  925. }
  926. static DEVICE_ATTR(prox0_raw_en, S_IRUGO | S_IWUSR, bh1770_prox_enable_show,
  927. bh1770_prox_enable_store);
  928. static DEVICE_ATTR(prox0_thresh_above1_value, S_IRUGO | S_IWUSR,
  929. bh1770_prox_abs_thres_show,
  930. bh1770_prox_abs_thres_store);
  931. static DEVICE_ATTR(prox0_thresh_above0_value, S_IRUGO | S_IWUSR,
  932. bh1770_get_prox_thres,
  933. bh1770_set_prox_thres);
  934. static DEVICE_ATTR(prox0_raw, S_IRUGO, bh1770_prox_result_show, NULL);
  935. static DEVICE_ATTR(prox0_sensor_range, S_IRUGO, bh1770_prox_range_show, NULL);
  936. static DEVICE_ATTR(prox0_thresh_above_count, S_IRUGO | S_IWUSR,
  937. bh1770_prox_persistence_show,
  938. bh1770_prox_persistence_store);
  939. static DEVICE_ATTR(prox0_rate_above, S_IRUGO | S_IWUSR,
  940. bh1770_get_prox_rate_above,
  941. bh1770_set_prox_rate_above);
  942. static DEVICE_ATTR(prox0_rate_below, S_IRUGO | S_IWUSR,
  943. bh1770_get_prox_rate_below,
  944. bh1770_set_prox_rate_below);
  945. static DEVICE_ATTR(prox0_rate_avail, S_IRUGO, bh1770_get_prox_rate_avail, NULL);
  946. static DEVICE_ATTR(lux0_calibscale, S_IRUGO | S_IWUSR, bh1770_lux_calib_show,
  947. bh1770_lux_calib_store);
  948. static DEVICE_ATTR(lux0_calibscale_default, S_IRUGO,
  949. bh1770_lux_calib_default_show,
  950. NULL);
  951. static DEVICE_ATTR(lux0_input, S_IRUGO, bh1770_lux_result_show, NULL);
  952. static DEVICE_ATTR(lux0_sensor_range, S_IRUGO, bh1770_lux_range_show, NULL);
  953. static DEVICE_ATTR(lux0_rate, S_IRUGO | S_IWUSR, bh1770_get_lux_rate,
  954. bh1770_set_lux_rate);
  955. static DEVICE_ATTR(lux0_rate_avail, S_IRUGO, bh1770_get_lux_rate_avail, NULL);
  956. static DEVICE_ATTR(lux0_thresh_above_value, S_IRUGO | S_IWUSR,
  957. bh1770_get_lux_thresh_above,
  958. bh1770_set_lux_thresh_above);
  959. static DEVICE_ATTR(lux0_thresh_below_value, S_IRUGO | S_IWUSR,
  960. bh1770_get_lux_thresh_below,
  961. bh1770_set_lux_thresh_below);
  962. static DEVICE_ATTR(chip_id, S_IRUGO, bh1770_chip_id_show, NULL);
  963. static DEVICE_ATTR(power_state, S_IRUGO | S_IWUSR, bh1770_power_state_show,
  964. bh1770_power_state_store);
  965. static struct attribute *sysfs_attrs[] = {
  966. &dev_attr_lux0_calibscale.attr,
  967. &dev_attr_lux0_calibscale_default.attr,
  968. &dev_attr_lux0_input.attr,
  969. &dev_attr_lux0_sensor_range.attr,
  970. &dev_attr_lux0_rate.attr,
  971. &dev_attr_lux0_rate_avail.attr,
  972. &dev_attr_lux0_thresh_above_value.attr,
  973. &dev_attr_lux0_thresh_below_value.attr,
  974. &dev_attr_prox0_raw.attr,
  975. &dev_attr_prox0_sensor_range.attr,
  976. &dev_attr_prox0_raw_en.attr,
  977. &dev_attr_prox0_thresh_above_count.attr,
  978. &dev_attr_prox0_rate_above.attr,
  979. &dev_attr_prox0_rate_below.attr,
  980. &dev_attr_prox0_rate_avail.attr,
  981. &dev_attr_prox0_thresh_above0_value.attr,
  982. &dev_attr_prox0_thresh_above1_value.attr,
  983. &dev_attr_chip_id.attr,
  984. &dev_attr_power_state.attr,
  985. NULL
  986. };
  987. static struct attribute_group bh1770_attribute_group = {
  988. .attrs = sysfs_attrs
  989. };
  990. static int __devinit bh1770_probe(struct i2c_client *client,
  991. const struct i2c_device_id *id)
  992. {
  993. struct bh1770_chip *chip;
  994. int err;
  995. chip = kzalloc(sizeof *chip, GFP_KERNEL);
  996. if (!chip)
  997. return -ENOMEM;
  998. i2c_set_clientdata(client, chip);
  999. chip->client = client;
  1000. mutex_init(&chip->mutex);
  1001. init_waitqueue_head(&chip->wait);
  1002. INIT_DELAYED_WORK(&chip->prox_work, bh1770_prox_work);
  1003. if (client->dev.platform_data == NULL) {
  1004. dev_err(&client->dev, "platform data is mandatory\n");
  1005. err = -EINVAL;
  1006. goto fail1;
  1007. }
  1008. chip->pdata = client->dev.platform_data;
  1009. chip->lux_calib = BH1770_LUX_NEUTRAL_CALIB_VALUE;
  1010. chip->lux_rate_index = BH1770_LUX_DEFAULT_RATE;
  1011. chip->lux_threshold_lo = BH1770_LUX_DEF_THRES;
  1012. chip->lux_threshold_hi = BH1770_LUX_DEF_THRES;
  1013. if (chip->pdata->glass_attenuation == 0)
  1014. chip->lux_ga = BH1770_NEUTRAL_GA;
  1015. else
  1016. chip->lux_ga = chip->pdata->glass_attenuation;
  1017. chip->prox_threshold = BH1770_PROX_DEF_THRES;
  1018. chip->prox_led = chip->pdata->led_def_curr;
  1019. chip->prox_abs_thres = BH1770_PROX_DEF_ABS_THRES;
  1020. chip->prox_persistence = BH1770_DEFAULT_PERSISTENCE;
  1021. chip->prox_rate_threshold = BH1770_PROX_DEF_RATE_THRESH;
  1022. chip->prox_rate = BH1770_PROX_DEFAULT_RATE;
  1023. chip->prox_data = 0;
  1024. chip->regs[0].supply = reg_vcc;
  1025. chip->regs[1].supply = reg_vleds;
  1026. err = regulator_bulk_get(&client->dev,
  1027. ARRAY_SIZE(chip->regs), chip->regs);
  1028. if (err < 0) {
  1029. dev_err(&client->dev, "Cannot get regulators\n");
  1030. goto fail1;
  1031. }
  1032. err = regulator_bulk_enable(ARRAY_SIZE(chip->regs),
  1033. chip->regs);
  1034. if (err < 0) {
  1035. dev_err(&client->dev, "Cannot enable regulators\n");
  1036. goto fail2;
  1037. }
  1038. usleep_range(BH1770_STARTUP_DELAY, BH1770_STARTUP_DELAY * 2);
  1039. err = bh1770_detect(chip);
  1040. if (err < 0)
  1041. goto fail3;
  1042. /* Start chip */
  1043. bh1770_chip_on(chip);
  1044. pm_runtime_set_active(&client->dev);
  1045. pm_runtime_enable(&client->dev);
  1046. chip->lux_corr = bh1770_get_corr_value(chip);
  1047. if (chip->lux_corr == 0) {
  1048. dev_err(&client->dev, "Improper correction values\n");
  1049. err = -EINVAL;
  1050. goto fail3;
  1051. }
  1052. if (chip->pdata->setup_resources) {
  1053. err = chip->pdata->setup_resources();
  1054. if (err) {
  1055. err = -EINVAL;
  1056. goto fail3;
  1057. }
  1058. }
  1059. err = sysfs_create_group(&chip->client->dev.kobj,
  1060. &bh1770_attribute_group);
  1061. if (err < 0) {
  1062. dev_err(&chip->client->dev, "Sysfs registration failed\n");
  1063. goto fail4;
  1064. }
  1065. /*
  1066. * Chip needs level triggered interrupt to work. However,
  1067. * level triggering doesn't work always correctly with power
  1068. * management. Select both
  1069. */
  1070. err = request_threaded_irq(client->irq, NULL,
  1071. bh1770_irq,
  1072. IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
  1073. IRQF_TRIGGER_LOW,
  1074. "bh1770", chip);
  1075. if (err) {
  1076. dev_err(&client->dev, "could not get IRQ %d\n",
  1077. client->irq);
  1078. goto fail5;
  1079. }
  1080. regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
  1081. return err;
  1082. fail5:
  1083. sysfs_remove_group(&chip->client->dev.kobj,
  1084. &bh1770_attribute_group);
  1085. fail4:
  1086. if (chip->pdata->release_resources)
  1087. chip->pdata->release_resources();
  1088. fail3:
  1089. regulator_bulk_disable(ARRAY_SIZE(chip->regs), chip->regs);
  1090. fail2:
  1091. regulator_bulk_free(ARRAY_SIZE(chip->regs), chip->regs);
  1092. fail1:
  1093. kfree(chip);
  1094. return err;
  1095. }
  1096. static int __devexit bh1770_remove(struct i2c_client *client)
  1097. {
  1098. struct bh1770_chip *chip = i2c_get_clientdata(client);
  1099. free_irq(client->irq, chip);
  1100. sysfs_remove_group(&chip->client->dev.kobj,
  1101. &bh1770_attribute_group);
  1102. if (chip->pdata->release_resources)
  1103. chip->pdata->release_resources();
  1104. cancel_delayed_work_sync(&chip->prox_work);
  1105. if (!pm_runtime_suspended(&client->dev))
  1106. bh1770_chip_off(chip);
  1107. pm_runtime_disable(&client->dev);
  1108. pm_runtime_set_suspended(&client->dev);
  1109. regulator_bulk_free(ARRAY_SIZE(chip->regs), chip->regs);
  1110. kfree(chip);
  1111. return 0;
  1112. }
  1113. #ifdef CONFIG_PM
  1114. static int bh1770_suspend(struct device *dev)
  1115. {
  1116. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  1117. struct bh1770_chip *chip = i2c_get_clientdata(client);
  1118. bh1770_chip_off(chip);
  1119. return 0;
  1120. }
  1121. static int bh1770_resume(struct device *dev)
  1122. {
  1123. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  1124. struct bh1770_chip *chip = i2c_get_clientdata(client);
  1125. int ret = 0;
  1126. bh1770_chip_on(chip);
  1127. if (!pm_runtime_suspended(dev)) {
  1128. /*
  1129. * If we were enabled at suspend time, it is expected
  1130. * everything works nice and smoothly
  1131. */
  1132. ret = bh1770_lux_rate(chip, chip->lux_rate_index);
  1133. ret |= bh1770_lux_interrupt_control(chip, BH1770_ENABLE);
  1134. /* This causes interrupt after the next measurement cycle */
  1135. bh1770_lux_update_thresholds(chip, BH1770_LUX_DEF_THRES,
  1136. BH1770_LUX_DEF_THRES);
  1137. /* Inform that we are waiting for a result from ALS */
  1138. chip->lux_wait_result = true;
  1139. bh1770_prox_mode_control(chip);
  1140. }
  1141. return ret;
  1142. }
  1143. #else
  1144. #define bh1770_suspend NULL
  1145. #define bh1770_shutdown NULL
  1146. #define bh1770_resume NULL
  1147. #endif
  1148. #ifdef CONFIG_PM_RUNTIME
  1149. static int bh1770_runtime_suspend(struct device *dev)
  1150. {
  1151. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  1152. struct bh1770_chip *chip = i2c_get_clientdata(client);
  1153. bh1770_chip_off(chip);
  1154. return 0;
  1155. }
  1156. static int bh1770_runtime_resume(struct device *dev)
  1157. {
  1158. struct i2c_client *client = container_of(dev, struct i2c_client, dev);
  1159. struct bh1770_chip *chip = i2c_get_clientdata(client);
  1160. bh1770_chip_on(chip);
  1161. return 0;
  1162. }
  1163. #endif
  1164. static const struct i2c_device_id bh1770_id[] = {
  1165. {"bh1770glc", 0 },
  1166. {"sfh7770", 0 },
  1167. {}
  1168. };
  1169. MODULE_DEVICE_TABLE(i2c, bh1770_id);
  1170. static const struct dev_pm_ops bh1770_pm_ops = {
  1171. SET_SYSTEM_SLEEP_PM_OPS(bh1770_suspend, bh1770_resume)
  1172. SET_RUNTIME_PM_OPS(bh1770_runtime_suspend, bh1770_runtime_resume, NULL)
  1173. };
  1174. static struct i2c_driver bh1770_driver = {
  1175. .driver = {
  1176. .name = "bh1770glc",
  1177. .owner = THIS_MODULE,
  1178. .pm = &bh1770_pm_ops,
  1179. },
  1180. .probe = bh1770_probe,
  1181. .remove = __devexit_p(bh1770_remove),
  1182. .id_table = bh1770_id,
  1183. };
  1184. static int __init bh1770_init(void)
  1185. {
  1186. return i2c_add_driver(&bh1770_driver);
  1187. }
  1188. static void __exit bh1770_exit(void)
  1189. {
  1190. i2c_del_driver(&bh1770_driver);
  1191. }
  1192. MODULE_DESCRIPTION("BH1770GLC / SFH7770 combined ALS and proximity sensor");
  1193. MODULE_AUTHOR("Samu Onkalo, Nokia Corporation");
  1194. MODULE_LICENSE("GPL v2");
  1195. module_init(bh1770_init);
  1196. module_exit(bh1770_exit);