adp8870_bl.c 27 KB

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