adp8870_bl.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. * Backlight driver for Analog Devices ADP8870 Backlight Devices
  3. *
  4. * Copyright 2009-2011 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/errno.h>
  11. #include <linux/pm.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/i2c.h>
  14. #include <linux/fb.h>
  15. #include <linux/backlight.h>
  16. #include <linux/leds.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/slab.h>
  19. #include <linux/i2c/adp8870.h>
  20. #define ADP8870_EXT_FEATURES
  21. #define ADP8870_USE_LEDS
  22. #define ADP8870_MFDVID 0x00 /* Manufacturer and device ID */
  23. #define ADP8870_MDCR 0x01 /* Device mode and status */
  24. #define ADP8870_INT_STAT 0x02 /* Interrupts status */
  25. #define ADP8870_INT_EN 0x03 /* Interrupts enable */
  26. #define ADP8870_CFGR 0x04 /* Configuration register */
  27. #define ADP8870_BLSEL 0x05 /* Sink enable backlight or independent */
  28. #define ADP8870_PWMLED 0x06 /* PWM Enable Selection Register */
  29. #define ADP8870_BLOFF 0x07 /* Backlight off timeout */
  30. #define ADP8870_BLDIM 0x08 /* Backlight dim timeout */
  31. #define ADP8870_BLFR 0x09 /* Backlight fade in and out rates */
  32. #define ADP8870_BLMX1 0x0A /* Backlight (Brightness Level 1-daylight) maximum current */
  33. #define ADP8870_BLDM1 0x0B /* Backlight (Brightness Level 1-daylight) dim current */
  34. #define ADP8870_BLMX2 0x0C /* Backlight (Brightness Level 2-bright) maximum current */
  35. #define ADP8870_BLDM2 0x0D /* Backlight (Brightness Level 2-bright) dim current */
  36. #define ADP8870_BLMX3 0x0E /* Backlight (Brightness Level 3-office) maximum current */
  37. #define ADP8870_BLDM3 0x0F /* Backlight (Brightness Level 3-office) dim current */
  38. #define ADP8870_BLMX4 0x10 /* Backlight (Brightness Level 4-indoor) maximum current */
  39. #define ADP8870_BLDM4 0x11 /* Backlight (Brightness Level 4-indoor) dim current */
  40. #define ADP8870_BLMX5 0x12 /* Backlight (Brightness Level 5-dark) maximum current */
  41. #define ADP8870_BLDM5 0x13 /* Backlight (Brightness Level 5-dark) dim current */
  42. #define ADP8870_ISCLAW 0x1A /* Independent sink current fade law register */
  43. #define ADP8870_ISCC 0x1B /* Independent sink current control register */
  44. #define ADP8870_ISCT1 0x1C /* Independent Sink Current Timer Register LED[7:5] */
  45. #define ADP8870_ISCT2 0x1D /* Independent Sink Current Timer Register LED[4:1] */
  46. #define ADP8870_ISCF 0x1E /* Independent sink current fade register */
  47. #define ADP8870_ISC1 0x1F /* Independent Sink Current LED1 */
  48. #define ADP8870_ISC2 0x20 /* Independent Sink Current LED2 */
  49. #define ADP8870_ISC3 0x21 /* Independent Sink Current LED3 */
  50. #define ADP8870_ISC4 0x22 /* Independent Sink Current LED4 */
  51. #define ADP8870_ISC5 0x23 /* Independent Sink Current LED5 */
  52. #define ADP8870_ISC6 0x24 /* Independent Sink Current LED6 */
  53. #define ADP8870_ISC7 0x25 /* Independent Sink Current LED7 (Brightness Level 1-daylight) */
  54. #define ADP8870_ISC7_L2 0x26 /* Independent Sink Current LED7 (Brightness Level 2-bright) */
  55. #define ADP8870_ISC7_L3 0x27 /* Independent Sink Current LED7 (Brightness Level 3-office) */
  56. #define ADP8870_ISC7_L4 0x28 /* Independent Sink Current LED7 (Brightness Level 4-indoor) */
  57. #define ADP8870_ISC7_L5 0x29 /* Independent Sink Current LED7 (Brightness Level 5-dark) */
  58. #define ADP8870_CMP_CTL 0x2D /* ALS Comparator Control Register */
  59. #define ADP8870_ALS1_EN 0x2E /* Main ALS comparator level enable */
  60. #define ADP8870_ALS2_EN 0x2F /* Second ALS comparator level enable */
  61. #define ADP8870_ALS1_STAT 0x30 /* Main ALS Comparator Status Register */
  62. #define ADP8870_ALS2_STAT 0x31 /* Second ALS Comparator Status Register */
  63. #define ADP8870_L2TRP 0x32 /* L2 comparator reference */
  64. #define ADP8870_L2HYS 0x33 /* L2 hysteresis */
  65. #define ADP8870_L3TRP 0x34 /* L3 comparator reference */
  66. #define ADP8870_L3HYS 0x35 /* L3 hysteresis */
  67. #define ADP8870_L4TRP 0x36 /* L4 comparator reference */
  68. #define ADP8870_L4HYS 0x37 /* L4 hysteresis */
  69. #define ADP8870_L5TRP 0x38 /* L5 comparator reference */
  70. #define ADP8870_L5HYS 0x39 /* L5 hysteresis */
  71. #define ADP8870_PH1LEVL 0x40 /* First phototransistor ambient light level-low byte register */
  72. #define ADP8870_PH1LEVH 0x41 /* First phototransistor ambient light level-high byte register */
  73. #define ADP8870_PH2LEVL 0x42 /* Second phototransistor ambient light level-low byte register */
  74. #define ADP8870_PH2LEVH 0x43 /* Second phototransistor ambient light level-high byte register */
  75. #define ADP8870_MANUFID 0x3 /* Analog Devices AD8870 Manufacturer and device ID */
  76. #define ADP8870_DEVID(x) ((x) & 0xF)
  77. #define ADP8870_MANID(x) ((x) >> 4)
  78. /* MDCR Device mode and status */
  79. #define D7ALSEN (1 << 7)
  80. #define INT_CFG (1 << 6)
  81. #define NSTBY (1 << 5)
  82. #define DIM_EN (1 << 4)
  83. #define GDWN_DIS (1 << 3)
  84. #define SIS_EN (1 << 2)
  85. #define CMP_AUTOEN (1 << 1)
  86. #define BLEN (1 << 0)
  87. /* ADP8870_ALS1_EN Main ALS comparator level enable */
  88. #define L5_EN (1 << 3)
  89. #define L4_EN (1 << 2)
  90. #define L3_EN (1 << 1)
  91. #define L2_EN (1 << 0)
  92. #define CFGR_BLV_SHIFT 3
  93. #define CFGR_BLV_MASK 0x7
  94. #define ADP8870_FLAG_LED_MASK 0xFF
  95. #define FADE_VAL(in, out) ((0xF & (in)) | ((0xF & (out)) << 4))
  96. #define BL_CFGR_VAL(law, blv) ((((blv) & CFGR_BLV_MASK) << CFGR_BLV_SHIFT) | ((0x3 & (law)) << 1))
  97. #define ALS_CMPR_CFG_VAL(filt) ((0x7 & (filt)) << 1)
  98. struct adp8870_bl {
  99. struct i2c_client *client;
  100. struct backlight_device *bl;
  101. struct adp8870_led *led;
  102. struct adp8870_backlight_platform_data *pdata;
  103. struct mutex lock;
  104. unsigned long cached_daylight_max;
  105. int id;
  106. int revid;
  107. int current_brightness;
  108. };
  109. struct adp8870_led {
  110. struct led_classdev cdev;
  111. struct work_struct work;
  112. struct i2c_client *client;
  113. enum led_brightness new_brightness;
  114. int id;
  115. int flags;
  116. };
  117. static int adp8870_read(struct i2c_client *client, int reg, uint8_t *val)
  118. {
  119. int ret;
  120. ret = i2c_smbus_read_byte_data(client, reg);
  121. if (ret < 0) {
  122. dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
  123. return ret;
  124. }
  125. *val = ret;
  126. return 0;
  127. }
  128. static int adp8870_write(struct i2c_client *client, u8 reg, u8 val)
  129. {
  130. int ret = i2c_smbus_write_byte_data(client, reg, val);
  131. if (ret)
  132. dev_err(&client->dev, "failed to write\n");
  133. return ret;
  134. }
  135. static int adp8870_set_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
  136. {
  137. struct adp8870_bl *data = i2c_get_clientdata(client);
  138. uint8_t reg_val;
  139. int ret;
  140. mutex_lock(&data->lock);
  141. ret = adp8870_read(client, reg, &reg_val);
  142. if (!ret && ((reg_val & bit_mask) != bit_mask)) {
  143. reg_val |= bit_mask;
  144. ret = adp8870_write(client, reg, reg_val);
  145. }
  146. mutex_unlock(&data->lock);
  147. return ret;
  148. }
  149. static int adp8870_clr_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
  150. {
  151. struct adp8870_bl *data = i2c_get_clientdata(client);
  152. uint8_t reg_val;
  153. int ret;
  154. mutex_lock(&data->lock);
  155. ret = adp8870_read(client, reg, &reg_val);
  156. if (!ret && (reg_val & bit_mask)) {
  157. reg_val &= ~bit_mask;
  158. ret = adp8870_write(client, reg, reg_val);
  159. }
  160. mutex_unlock(&data->lock);
  161. return ret;
  162. }
  163. /*
  164. * Independent sink / LED
  165. */
  166. #if defined(ADP8870_USE_LEDS)
  167. static void adp8870_led_work(struct work_struct *work)
  168. {
  169. struct adp8870_led *led = container_of(work, struct adp8870_led, work);
  170. adp8870_write(led->client, ADP8870_ISC1 + led->id - 1,
  171. led->new_brightness >> 1);
  172. }
  173. static void adp8870_led_set(struct led_classdev *led_cdev,
  174. enum led_brightness value)
  175. {
  176. struct adp8870_led *led;
  177. led = container_of(led_cdev, struct adp8870_led, cdev);
  178. led->new_brightness = value;
  179. /*
  180. * Use workqueue for IO since I2C operations can sleep.
  181. */
  182. schedule_work(&led->work);
  183. }
  184. static int adp8870_led_setup(struct adp8870_led *led)
  185. {
  186. struct i2c_client *client = led->client;
  187. int ret = 0;
  188. ret = adp8870_write(client, ADP8870_ISC1 + led->id - 1, 0);
  189. if (ret)
  190. return ret;
  191. ret = adp8870_set_bits(client, ADP8870_ISCC, 1 << (led->id - 1));
  192. if (ret)
  193. return ret;
  194. if (led->id > 4)
  195. ret = adp8870_set_bits(client, ADP8870_ISCT1,
  196. (led->flags & 0x3) << ((led->id - 5) * 2));
  197. else
  198. ret = adp8870_set_bits(client, ADP8870_ISCT2,
  199. (led->flags & 0x3) << ((led->id - 1) * 2));
  200. return ret;
  201. }
  202. static int __devinit adp8870_led_probe(struct i2c_client *client)
  203. {
  204. struct adp8870_backlight_platform_data *pdata =
  205. client->dev.platform_data;
  206. struct adp8870_bl *data = i2c_get_clientdata(client);
  207. struct adp8870_led *led, *led_dat;
  208. struct led_info *cur_led;
  209. int ret, i;
  210. led = kcalloc(pdata->num_leds, sizeof(*led), GFP_KERNEL);
  211. if (led == NULL) {
  212. dev_err(&client->dev, "failed to alloc memory\n");
  213. return -ENOMEM;
  214. }
  215. ret = adp8870_write(client, ADP8870_ISCLAW, pdata->led_fade_law);
  216. if (ret)
  217. goto err_free;
  218. ret = adp8870_write(client, ADP8870_ISCT1,
  219. (pdata->led_on_time & 0x3) << 6);
  220. if (ret)
  221. goto err_free;
  222. ret = adp8870_write(client, ADP8870_ISCF,
  223. FADE_VAL(pdata->led_fade_in, pdata->led_fade_out));
  224. if (ret)
  225. goto err_free;
  226. for (i = 0; i < pdata->num_leds; ++i) {
  227. cur_led = &pdata->leds[i];
  228. led_dat = &led[i];
  229. led_dat->id = cur_led->flags & ADP8870_FLAG_LED_MASK;
  230. if (led_dat->id > 7 || led_dat->id < 1) {
  231. dev_err(&client->dev, "Invalid LED ID %d\n",
  232. led_dat->id);
  233. goto err;
  234. }
  235. if (pdata->bl_led_assign & (1 << (led_dat->id - 1))) {
  236. dev_err(&client->dev, "LED %d used by Backlight\n",
  237. led_dat->id);
  238. goto err;
  239. }
  240. led_dat->cdev.name = cur_led->name;
  241. led_dat->cdev.default_trigger = cur_led->default_trigger;
  242. led_dat->cdev.brightness_set = adp8870_led_set;
  243. led_dat->cdev.brightness = LED_OFF;
  244. led_dat->flags = cur_led->flags >> FLAG_OFFT_SHIFT;
  245. led_dat->client = client;
  246. led_dat->new_brightness = LED_OFF;
  247. INIT_WORK(&led_dat->work, adp8870_led_work);
  248. ret = led_classdev_register(&client->dev, &led_dat->cdev);
  249. if (ret) {
  250. dev_err(&client->dev, "failed to register LED %d\n",
  251. led_dat->id);
  252. goto err;
  253. }
  254. ret = adp8870_led_setup(led_dat);
  255. if (ret) {
  256. dev_err(&client->dev, "failed to write\n");
  257. i++;
  258. goto err;
  259. }
  260. }
  261. data->led = led;
  262. return 0;
  263. err:
  264. for (i = i - 1; i >= 0; --i) {
  265. led_classdev_unregister(&led[i].cdev);
  266. cancel_work_sync(&led[i].work);
  267. }
  268. err_free:
  269. kfree(led);
  270. return ret;
  271. }
  272. static int __devexit adp8870_led_remove(struct i2c_client *client)
  273. {
  274. struct adp8870_backlight_platform_data *pdata =
  275. client->dev.platform_data;
  276. struct adp8870_bl *data = i2c_get_clientdata(client);
  277. int i;
  278. for (i = 0; i < pdata->num_leds; i++) {
  279. led_classdev_unregister(&data->led[i].cdev);
  280. cancel_work_sync(&data->led[i].work);
  281. }
  282. kfree(data->led);
  283. return 0;
  284. }
  285. #else
  286. static int __devinit adp8870_led_probe(struct i2c_client *client)
  287. {
  288. return 0;
  289. }
  290. static int __devexit adp8870_led_remove(struct i2c_client *client)
  291. {
  292. return 0;
  293. }
  294. #endif
  295. static int adp8870_bl_set(struct backlight_device *bl, int brightness)
  296. {
  297. struct adp8870_bl *data = bl_get_data(bl);
  298. struct i2c_client *client = data->client;
  299. int ret = 0;
  300. if (data->pdata->en_ambl_sens) {
  301. if ((brightness > 0) && (brightness < ADP8870_MAX_BRIGHTNESS)) {
  302. /* Disable Ambient Light auto adjust */
  303. ret = adp8870_clr_bits(client, ADP8870_MDCR,
  304. CMP_AUTOEN);
  305. if (ret)
  306. return ret;
  307. ret = adp8870_write(client, ADP8870_BLMX1, brightness);
  308. if (ret)
  309. return ret;
  310. } else {
  311. /*
  312. * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
  313. * restore daylight l1 sysfs brightness
  314. */
  315. ret = adp8870_write(client, ADP8870_BLMX1,
  316. data->cached_daylight_max);
  317. if (ret)
  318. return ret;
  319. ret = adp8870_set_bits(client, ADP8870_MDCR,
  320. CMP_AUTOEN);
  321. if (ret)
  322. return ret;
  323. }
  324. } else {
  325. ret = adp8870_write(client, ADP8870_BLMX1, brightness);
  326. if (ret)
  327. return ret;
  328. }
  329. if (data->current_brightness && brightness == 0)
  330. ret = adp8870_set_bits(client,
  331. ADP8870_MDCR, DIM_EN);
  332. else if (data->current_brightness == 0 && brightness)
  333. ret = adp8870_clr_bits(client,
  334. ADP8870_MDCR, DIM_EN);
  335. if (!ret)
  336. data->current_brightness = brightness;
  337. return ret;
  338. }
  339. static int adp8870_bl_update_status(struct backlight_device *bl)
  340. {
  341. int brightness = bl->props.brightness;
  342. if (bl->props.power != FB_BLANK_UNBLANK)
  343. brightness = 0;
  344. if (bl->props.fb_blank != FB_BLANK_UNBLANK)
  345. brightness = 0;
  346. return adp8870_bl_set(bl, brightness);
  347. }
  348. static int adp8870_bl_get_brightness(struct backlight_device *bl)
  349. {
  350. struct adp8870_bl *data = bl_get_data(bl);
  351. return data->current_brightness;
  352. }
  353. static const struct backlight_ops adp8870_bl_ops = {
  354. .update_status = adp8870_bl_update_status,
  355. .get_brightness = adp8870_bl_get_brightness,
  356. };
  357. static int adp8870_bl_setup(struct backlight_device *bl)
  358. {
  359. struct adp8870_bl *data = bl_get_data(bl);
  360. struct i2c_client *client = data->client;
  361. struct adp8870_backlight_platform_data *pdata = data->pdata;
  362. int ret = 0;
  363. ret = adp8870_write(client, ADP8870_BLSEL, ~pdata->bl_led_assign);
  364. if (ret)
  365. return ret;
  366. ret = adp8870_write(client, ADP8870_PWMLED, pdata->pwm_assign);
  367. if (ret)
  368. return ret;
  369. ret = adp8870_write(client, ADP8870_BLMX1, pdata->l1_daylight_max);
  370. if (ret)
  371. return ret;
  372. ret = adp8870_write(client, ADP8870_BLDM1, pdata->l1_daylight_dim);
  373. if (ret)
  374. return ret;
  375. if (pdata->en_ambl_sens) {
  376. data->cached_daylight_max = pdata->l1_daylight_max;
  377. ret = adp8870_write(client, ADP8870_BLMX2,
  378. pdata->l2_bright_max);
  379. if (ret)
  380. return ret;
  381. ret = adp8870_write(client, ADP8870_BLDM2,
  382. pdata->l2_bright_dim);
  383. if (ret)
  384. return ret;
  385. ret = adp8870_write(client, ADP8870_BLMX3,
  386. pdata->l3_office_max);
  387. if (ret)
  388. return ret;
  389. ret = adp8870_write(client, ADP8870_BLDM3,
  390. pdata->l3_office_dim);
  391. if (ret)
  392. return ret;
  393. ret = adp8870_write(client, ADP8870_BLMX4,
  394. pdata->l4_indoor_max);
  395. if (ret)
  396. return ret;
  397. ret = adp8870_write(client, ADP8870_BLDM4,
  398. pdata->l4_indor_dim);
  399. if (ret)
  400. return ret;
  401. ret = adp8870_write(client, ADP8870_BLMX5,
  402. pdata->l5_dark_max);
  403. if (ret)
  404. return ret;
  405. ret = adp8870_write(client, ADP8870_BLDM5,
  406. pdata->l5_dark_dim);
  407. if (ret)
  408. return ret;
  409. ret = adp8870_write(client, ADP8870_L2TRP, pdata->l2_trip);
  410. if (ret)
  411. return ret;
  412. ret = adp8870_write(client, ADP8870_L2HYS, pdata->l2_hyst);
  413. if (ret)
  414. return ret;
  415. ret = adp8870_write(client, ADP8870_L3TRP, pdata->l3_trip);
  416. if (ret)
  417. return ret;
  418. ret = adp8870_write(client, ADP8870_L3HYS, pdata->l3_hyst);
  419. if (ret)
  420. return ret;
  421. ret = adp8870_write(client, ADP8870_L4TRP, pdata->l4_trip);
  422. if (ret)
  423. return ret;
  424. ret = adp8870_write(client, ADP8870_L4HYS, pdata->l4_hyst);
  425. if (ret)
  426. return ret;
  427. ret = adp8870_write(client, ADP8870_L5TRP, pdata->l5_trip);
  428. if (ret)
  429. return ret;
  430. ret = adp8870_write(client, ADP8870_L5HYS, pdata->l5_hyst);
  431. if (ret)
  432. return ret;
  433. ret = adp8870_write(client, ADP8870_ALS1_EN, L5_EN | L4_EN |
  434. L3_EN | L2_EN);
  435. if (ret)
  436. return ret;
  437. ret = adp8870_write(client, ADP8870_CMP_CTL,
  438. ALS_CMPR_CFG_VAL(pdata->abml_filt));
  439. if (ret)
  440. return ret;
  441. }
  442. ret = adp8870_write(client, ADP8870_CFGR,
  443. BL_CFGR_VAL(pdata->bl_fade_law, 0));
  444. if (ret)
  445. return ret;
  446. ret = adp8870_write(client, ADP8870_BLFR, FADE_VAL(pdata->bl_fade_in,
  447. pdata->bl_fade_out));
  448. if (ret)
  449. return ret;
  450. /*
  451. * ADP8870 Rev0 requires GDWN_DIS bit set
  452. */
  453. ret = adp8870_set_bits(client, ADP8870_MDCR, BLEN | DIM_EN | NSTBY |
  454. (data->revid == 0 ? GDWN_DIS : 0));
  455. return ret;
  456. }
  457. static ssize_t adp8870_show(struct device *dev, char *buf, int reg)
  458. {
  459. struct adp8870_bl *data = dev_get_drvdata(dev);
  460. int error;
  461. uint8_t reg_val;
  462. mutex_lock(&data->lock);
  463. error = adp8870_read(data->client, reg, &reg_val);
  464. mutex_unlock(&data->lock);
  465. if (error < 0)
  466. return error;
  467. return sprintf(buf, "%u\n", reg_val);
  468. }
  469. static ssize_t adp8870_store(struct device *dev, const char *buf,
  470. size_t count, int reg)
  471. {
  472. struct adp8870_bl *data = dev_get_drvdata(dev);
  473. unsigned long val;
  474. int ret;
  475. ret = strict_strtoul(buf, 10, &val);
  476. if (ret)
  477. return ret;
  478. mutex_lock(&data->lock);
  479. adp8870_write(data->client, reg, val);
  480. mutex_unlock(&data->lock);
  481. return count;
  482. }
  483. static ssize_t adp8870_bl_l5_dark_max_show(struct device *dev,
  484. struct device_attribute *attr, char *buf)
  485. {
  486. return adp8870_show(dev, buf, ADP8870_BLMX5);
  487. }
  488. static ssize_t adp8870_bl_l5_dark_max_store(struct device *dev,
  489. struct device_attribute *attr, const char *buf, size_t count)
  490. {
  491. return adp8870_store(dev, buf, count, ADP8870_BLMX5);
  492. }
  493. static DEVICE_ATTR(l5_dark_max, 0664, adp8870_bl_l5_dark_max_show,
  494. adp8870_bl_l5_dark_max_store);
  495. static ssize_t adp8870_bl_l4_indoor_max_show(struct device *dev,
  496. struct device_attribute *attr, char *buf)
  497. {
  498. return adp8870_show(dev, buf, ADP8870_BLMX4);
  499. }
  500. static ssize_t adp8870_bl_l4_indoor_max_store(struct device *dev,
  501. struct device_attribute *attr, const char *buf, size_t count)
  502. {
  503. return adp8870_store(dev, buf, count, ADP8870_BLMX4);
  504. }
  505. static DEVICE_ATTR(l4_indoor_max, 0664, adp8870_bl_l4_indoor_max_show,
  506. adp8870_bl_l4_indoor_max_store);
  507. static ssize_t adp8870_bl_l3_office_max_show(struct device *dev,
  508. struct device_attribute *attr, char *buf)
  509. {
  510. return adp8870_show(dev, buf, ADP8870_BLMX3);
  511. }
  512. static ssize_t adp8870_bl_l3_office_max_store(struct device *dev,
  513. struct device_attribute *attr, const char *buf, size_t count)
  514. {
  515. return adp8870_store(dev, buf, count, ADP8870_BLMX3);
  516. }
  517. static DEVICE_ATTR(l3_office_max, 0664, adp8870_bl_l3_office_max_show,
  518. adp8870_bl_l3_office_max_store);
  519. static ssize_t adp8870_bl_l2_bright_max_show(struct device *dev,
  520. struct device_attribute *attr, char *buf)
  521. {
  522. return adp8870_show(dev, buf, ADP8870_BLMX2);
  523. }
  524. static ssize_t adp8870_bl_l2_bright_max_store(struct device *dev,
  525. struct device_attribute *attr, const char *buf, size_t count)
  526. {
  527. return adp8870_store(dev, buf, count, ADP8870_BLMX2);
  528. }
  529. static DEVICE_ATTR(l2_bright_max, 0664, adp8870_bl_l2_bright_max_show,
  530. adp8870_bl_l2_bright_max_store);
  531. static ssize_t adp8870_bl_l1_daylight_max_show(struct device *dev,
  532. struct device_attribute *attr, char *buf)
  533. {
  534. return adp8870_show(dev, buf, ADP8870_BLMX1);
  535. }
  536. static ssize_t adp8870_bl_l1_daylight_max_store(struct device *dev,
  537. struct device_attribute *attr, const char *buf, size_t count)
  538. {
  539. struct adp8870_bl *data = dev_get_drvdata(dev);
  540. int ret = strict_strtoul(buf, 10, &data->cached_daylight_max);
  541. if (ret)
  542. return ret;
  543. return adp8870_store(dev, buf, count, ADP8870_BLMX1);
  544. }
  545. static DEVICE_ATTR(l1_daylight_max, 0664, adp8870_bl_l1_daylight_max_show,
  546. adp8870_bl_l1_daylight_max_store);
  547. static ssize_t adp8870_bl_l5_dark_dim_show(struct device *dev,
  548. struct device_attribute *attr, char *buf)
  549. {
  550. return adp8870_show(dev, buf, ADP8870_BLDM5);
  551. }
  552. static ssize_t adp8870_bl_l5_dark_dim_store(struct device *dev,
  553. struct device_attribute *attr,
  554. const char *buf, size_t count)
  555. {
  556. return adp8870_store(dev, buf, count, ADP8870_BLDM5);
  557. }
  558. static DEVICE_ATTR(l5_dark_dim, 0664, adp8870_bl_l5_dark_dim_show,
  559. adp8870_bl_l5_dark_dim_store);
  560. static ssize_t adp8870_bl_l4_indoor_dim_show(struct device *dev,
  561. struct device_attribute *attr, char *buf)
  562. {
  563. return adp8870_show(dev, buf, ADP8870_BLDM4);
  564. }
  565. static ssize_t adp8870_bl_l4_indoor_dim_store(struct device *dev,
  566. struct device_attribute *attr,
  567. const char *buf, size_t count)
  568. {
  569. return adp8870_store(dev, buf, count, ADP8870_BLDM4);
  570. }
  571. static DEVICE_ATTR(l4_indoor_dim, 0664, adp8870_bl_l4_indoor_dim_show,
  572. adp8870_bl_l4_indoor_dim_store);
  573. static ssize_t adp8870_bl_l3_office_dim_show(struct device *dev,
  574. struct device_attribute *attr, char *buf)
  575. {
  576. return adp8870_show(dev, buf, ADP8870_BLDM3);
  577. }
  578. static ssize_t adp8870_bl_l3_office_dim_store(struct device *dev,
  579. struct device_attribute *attr,
  580. const char *buf, size_t count)
  581. {
  582. return adp8870_store(dev, buf, count, ADP8870_BLDM3);
  583. }
  584. static DEVICE_ATTR(l3_office_dim, 0664, adp8870_bl_l3_office_dim_show,
  585. adp8870_bl_l3_office_dim_store);
  586. static ssize_t adp8870_bl_l2_bright_dim_show(struct device *dev,
  587. struct device_attribute *attr, char *buf)
  588. {
  589. return adp8870_show(dev, buf, ADP8870_BLDM2);
  590. }
  591. static ssize_t adp8870_bl_l2_bright_dim_store(struct device *dev,
  592. struct device_attribute *attr,
  593. const char *buf, size_t count)
  594. {
  595. return adp8870_store(dev, buf, count, ADP8870_BLDM2);
  596. }
  597. static DEVICE_ATTR(l2_bright_dim, 0664, adp8870_bl_l2_bright_dim_show,
  598. adp8870_bl_l2_bright_dim_store);
  599. static ssize_t adp8870_bl_l1_daylight_dim_show(struct device *dev,
  600. struct device_attribute *attr, char *buf)
  601. {
  602. return adp8870_show(dev, buf, ADP8870_BLDM1);
  603. }
  604. static ssize_t adp8870_bl_l1_daylight_dim_store(struct device *dev,
  605. struct device_attribute *attr,
  606. const char *buf, size_t count)
  607. {
  608. return adp8870_store(dev, buf, count, ADP8870_BLDM1);
  609. }
  610. static DEVICE_ATTR(l1_daylight_dim, 0664, adp8870_bl_l1_daylight_dim_show,
  611. adp8870_bl_l1_daylight_dim_store);
  612. #ifdef ADP8870_EXT_FEATURES
  613. static ssize_t adp8870_bl_ambient_light_level_show(struct device *dev,
  614. struct device_attribute *attr, char *buf)
  615. {
  616. struct adp8870_bl *data = dev_get_drvdata(dev);
  617. int error;
  618. uint8_t reg_val;
  619. uint16_t ret_val;
  620. mutex_lock(&data->lock);
  621. error = adp8870_read(data->client, ADP8870_PH1LEVL, &reg_val);
  622. if (error < 0) {
  623. mutex_unlock(&data->lock);
  624. return error;
  625. }
  626. ret_val = reg_val;
  627. error = adp8870_read(data->client, ADP8870_PH1LEVH, &reg_val);
  628. mutex_unlock(&data->lock);
  629. if (error < 0)
  630. return error;
  631. /* Return 13-bit conversion value for the first light sensor */
  632. ret_val += (reg_val & 0x1F) << 8;
  633. return sprintf(buf, "%u\n", ret_val);
  634. }
  635. static DEVICE_ATTR(ambient_light_level, 0444,
  636. adp8870_bl_ambient_light_level_show, NULL);
  637. static ssize_t adp8870_bl_ambient_light_zone_show(struct device *dev,
  638. struct device_attribute *attr, char *buf)
  639. {
  640. struct adp8870_bl *data = dev_get_drvdata(dev);
  641. int error;
  642. uint8_t reg_val;
  643. mutex_lock(&data->lock);
  644. error = adp8870_read(data->client, ADP8870_CFGR, &reg_val);
  645. mutex_unlock(&data->lock);
  646. if (error < 0)
  647. return error;
  648. return sprintf(buf, "%u\n",
  649. ((reg_val >> CFGR_BLV_SHIFT) & CFGR_BLV_MASK) + 1);
  650. }
  651. static ssize_t adp8870_bl_ambient_light_zone_store(struct device *dev,
  652. struct device_attribute *attr,
  653. const char *buf, size_t count)
  654. {
  655. struct adp8870_bl *data = dev_get_drvdata(dev);
  656. unsigned long val;
  657. uint8_t reg_val;
  658. int ret;
  659. ret = strict_strtoul(buf, 10, &val);
  660. if (ret)
  661. return ret;
  662. if (val == 0) {
  663. /* Enable automatic ambient light sensing */
  664. adp8870_set_bits(data->client, ADP8870_MDCR, CMP_AUTOEN);
  665. } else if ((val > 0) && (val < 6)) {
  666. /* Disable automatic ambient light sensing */
  667. adp8870_clr_bits(data->client, ADP8870_MDCR, CMP_AUTOEN);
  668. /* Set user supplied ambient light zone */
  669. mutex_lock(&data->lock);
  670. adp8870_read(data->client, ADP8870_CFGR, &reg_val);
  671. reg_val &= ~(CFGR_BLV_MASK << CFGR_BLV_SHIFT);
  672. reg_val |= (val - 1) << CFGR_BLV_SHIFT;
  673. adp8870_write(data->client, ADP8870_CFGR, reg_val);
  674. mutex_unlock(&data->lock);
  675. }
  676. return count;
  677. }
  678. static DEVICE_ATTR(ambient_light_zone, 0664,
  679. adp8870_bl_ambient_light_zone_show,
  680. adp8870_bl_ambient_light_zone_store);
  681. #endif
  682. static struct attribute *adp8870_bl_attributes[] = {
  683. &dev_attr_l5_dark_max.attr,
  684. &dev_attr_l5_dark_dim.attr,
  685. &dev_attr_l4_indoor_max.attr,
  686. &dev_attr_l4_indoor_dim.attr,
  687. &dev_attr_l3_office_max.attr,
  688. &dev_attr_l3_office_dim.attr,
  689. &dev_attr_l2_bright_max.attr,
  690. &dev_attr_l2_bright_dim.attr,
  691. &dev_attr_l1_daylight_max.attr,
  692. &dev_attr_l1_daylight_dim.attr,
  693. #ifdef ADP8870_EXT_FEATURES
  694. &dev_attr_ambient_light_level.attr,
  695. &dev_attr_ambient_light_zone.attr,
  696. #endif
  697. NULL
  698. };
  699. static const struct attribute_group adp8870_bl_attr_group = {
  700. .attrs = adp8870_bl_attributes,
  701. };
  702. static int __devinit adp8870_probe(struct i2c_client *client,
  703. const struct i2c_device_id *id)
  704. {
  705. struct backlight_properties props;
  706. struct backlight_device *bl;
  707. struct adp8870_bl *data;
  708. struct adp8870_backlight_platform_data *pdata =
  709. client->dev.platform_data;
  710. uint8_t reg_val;
  711. int ret;
  712. if (!i2c_check_functionality(client->adapter,
  713. I2C_FUNC_SMBUS_BYTE_DATA)) {
  714. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  715. return -EIO;
  716. }
  717. if (!pdata) {
  718. dev_err(&client->dev, "no platform data?\n");
  719. return -EINVAL;
  720. }
  721. ret = adp8870_read(client, ADP8870_MFDVID, &reg_val);
  722. if (ret < 0)
  723. return -EIO;
  724. if (ADP8870_MANID(reg_val) != ADP8870_MANUFID) {
  725. dev_err(&client->dev, "failed to probe\n");
  726. return -ENODEV;
  727. }
  728. data = kzalloc(sizeof(*data), GFP_KERNEL);
  729. if (data == NULL)
  730. return -ENOMEM;
  731. data->revid = ADP8870_DEVID(reg_val);
  732. data->client = client;
  733. data->pdata = pdata;
  734. data->id = id->driver_data;
  735. data->current_brightness = 0;
  736. i2c_set_clientdata(client, data);
  737. mutex_init(&data->lock);
  738. memset(&props, 0, sizeof(props));
  739. props.type = BACKLIGHT_RAW;
  740. props.max_brightness = props.brightness = ADP8870_MAX_BRIGHTNESS;
  741. bl = backlight_device_register(dev_driver_string(&client->dev),
  742. &client->dev, data, &adp8870_bl_ops, &props);
  743. if (IS_ERR(bl)) {
  744. dev_err(&client->dev, "failed to register backlight\n");
  745. ret = PTR_ERR(bl);
  746. goto out2;
  747. }
  748. data->bl = bl;
  749. if (pdata->en_ambl_sens)
  750. ret = sysfs_create_group(&bl->dev.kobj,
  751. &adp8870_bl_attr_group);
  752. if (ret) {
  753. dev_err(&client->dev, "failed to register sysfs\n");
  754. goto out1;
  755. }
  756. ret = adp8870_bl_setup(bl);
  757. if (ret) {
  758. ret = -EIO;
  759. goto out;
  760. }
  761. backlight_update_status(bl);
  762. dev_info(&client->dev, "Rev.%d Backlight\n", data->revid);
  763. if (pdata->num_leds)
  764. adp8870_led_probe(client);
  765. return 0;
  766. out:
  767. if (data->pdata->en_ambl_sens)
  768. sysfs_remove_group(&data->bl->dev.kobj,
  769. &adp8870_bl_attr_group);
  770. out1:
  771. backlight_device_unregister(bl);
  772. out2:
  773. kfree(data);
  774. return ret;
  775. }
  776. static int __devexit adp8870_remove(struct i2c_client *client)
  777. {
  778. struct adp8870_bl *data = i2c_get_clientdata(client);
  779. adp8870_clr_bits(client, ADP8870_MDCR, NSTBY);
  780. if (data->led)
  781. adp8870_led_remove(client);
  782. if (data->pdata->en_ambl_sens)
  783. sysfs_remove_group(&data->bl->dev.kobj,
  784. &adp8870_bl_attr_group);
  785. backlight_device_unregister(data->bl);
  786. kfree(data);
  787. return 0;
  788. }
  789. #ifdef CONFIG_PM
  790. static int adp8870_i2c_suspend(struct i2c_client *client, pm_message_t message)
  791. {
  792. adp8870_clr_bits(client, ADP8870_MDCR, NSTBY);
  793. return 0;
  794. }
  795. static int adp8870_i2c_resume(struct i2c_client *client)
  796. {
  797. adp8870_set_bits(client, ADP8870_MDCR, NSTBY);
  798. return 0;
  799. }
  800. #else
  801. #define adp8870_i2c_suspend NULL
  802. #define adp8870_i2c_resume NULL
  803. #endif
  804. static const struct i2c_device_id adp8870_id[] = {
  805. { "adp8870", 0 },
  806. { }
  807. };
  808. MODULE_DEVICE_TABLE(i2c, adp8870_id);
  809. static struct i2c_driver adp8870_driver = {
  810. .driver = {
  811. .name = KBUILD_MODNAME,
  812. },
  813. .probe = adp8870_probe,
  814. .remove = __devexit_p(adp8870_remove),
  815. .suspend = adp8870_i2c_suspend,
  816. .resume = adp8870_i2c_resume,
  817. .id_table = adp8870_id,
  818. };
  819. static int __init adp8870_init(void)
  820. {
  821. return i2c_add_driver(&adp8870_driver);
  822. }
  823. module_init(adp8870_init);
  824. static void __exit adp8870_exit(void)
  825. {
  826. i2c_del_driver(&adp8870_driver);
  827. }
  828. module_exit(adp8870_exit);
  829. MODULE_LICENSE("GPL v2");
  830. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  831. MODULE_DESCRIPTION("ADP8870 Backlight driver");
  832. MODULE_ALIAS("i2c:adp8870-backlight");