adp8860_bl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /*
  2. * Backlight driver for Analog Devices ADP8860 Backlight Devices
  3. *
  4. * Copyright 2009-2010 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/slab.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/i2c/adp8860.h>
  20. #define ADP8860_EXT_FEATURES
  21. #define ADP8860_USE_LEDS
  22. #define ADP8860_MFDVID 0x00 /* Manufacturer and device ID */
  23. #define ADP8860_MDCR 0x01 /* Device mode and status */
  24. #define ADP8860_MDCR2 0x02 /* Device mode and Status Register 2 */
  25. #define ADP8860_INTR_EN 0x03 /* Interrupts enable */
  26. #define ADP8860_CFGR 0x04 /* Configuration register */
  27. #define ADP8860_BLSEN 0x05 /* Sink enable backlight or independent */
  28. #define ADP8860_BLOFF 0x06 /* Backlight off timeout */
  29. #define ADP8860_BLDIM 0x07 /* Backlight dim timeout */
  30. #define ADP8860_BLFR 0x08 /* Backlight fade in and out rates */
  31. #define ADP8860_BLMX1 0x09 /* Backlight (Brightness Level 1-daylight) maximum current */
  32. #define ADP8860_BLDM1 0x0A /* Backlight (Brightness Level 1-daylight) dim current */
  33. #define ADP8860_BLMX2 0x0B /* Backlight (Brightness Level 2-office) maximum current */
  34. #define ADP8860_BLDM2 0x0C /* Backlight (Brightness Level 2-office) dim current */
  35. #define ADP8860_BLMX3 0x0D /* Backlight (Brightness Level 3-dark) maximum current */
  36. #define ADP8860_BLDM3 0x0E /* Backlight (Brightness Level 3-dark) dim current */
  37. #define ADP8860_ISCFR 0x0F /* Independent sink current fade control register */
  38. #define ADP8860_ISCC 0x10 /* Independent sink current control register */
  39. #define ADP8860_ISCT1 0x11 /* Independent Sink Current Timer Register LED[7:5] */
  40. #define ADP8860_ISCT2 0x12 /* Independent Sink Current Timer Register LED[4:1] */
  41. #define ADP8860_ISCF 0x13 /* Independent sink current fade register */
  42. #define ADP8860_ISC7 0x14 /* Independent Sink Current LED7 */
  43. #define ADP8860_ISC6 0x15 /* Independent Sink Current LED6 */
  44. #define ADP8860_ISC5 0x16 /* Independent Sink Current LED5 */
  45. #define ADP8860_ISC4 0x17 /* Independent Sink Current LED4 */
  46. #define ADP8860_ISC3 0x18 /* Independent Sink Current LED3 */
  47. #define ADP8860_ISC2 0x19 /* Independent Sink Current LED2 */
  48. #define ADP8860_ISC1 0x1A /* Independent Sink Current LED1 */
  49. #define ADP8860_CCFG 0x1B /* Comparator configuration */
  50. #define ADP8860_CCFG2 0x1C /* Second comparator configuration */
  51. #define ADP8860_L2_TRP 0x1D /* L2 comparator reference */
  52. #define ADP8860_L2_HYS 0x1E /* L2 hysteresis */
  53. #define ADP8860_L3_TRP 0x1F /* L3 comparator reference */
  54. #define ADP8860_L3_HYS 0x20 /* L3 hysteresis */
  55. #define ADP8860_PH1LEVL 0x21 /* First phototransistor ambient light level-low byte register */
  56. #define ADP8860_PH1LEVH 0x22 /* First phototransistor ambient light level-high byte register */
  57. #define ADP8860_PH2LEVL 0x23 /* Second phototransistor ambient light level-low byte register */
  58. #define ADP8860_PH2LEVH 0x24 /* Second phototransistor ambient light level-high byte register */
  59. #define ADP8860_MANUFID 0x0 /* Analog Devices ADP8860 Manufacturer ID */
  60. #define ADP8861_MANUFID 0x4 /* Analog Devices ADP8861 Manufacturer ID */
  61. #define ADP8863_MANUFID 0x2 /* Analog Devices ADP8863 Manufacturer ID */
  62. #define ADP8860_DEVID(x) ((x) & 0xF)
  63. #define ADP8860_MANID(x) ((x) >> 4)
  64. /* MDCR Device mode and status */
  65. #define INT_CFG (1 << 6)
  66. #define NSTBY (1 << 5)
  67. #define DIM_EN (1 << 4)
  68. #define GDWN_DIS (1 << 3)
  69. #define SIS_EN (1 << 2)
  70. #define CMP_AUTOEN (1 << 1)
  71. #define BLEN (1 << 0)
  72. /* ADP8860_CCFG Main ALS comparator level enable */
  73. #define L3_EN (1 << 1)
  74. #define L2_EN (1 << 0)
  75. #define CFGR_BLV_SHIFT 3
  76. #define CFGR_BLV_MASK 0x3
  77. #define ADP8860_FLAG_LED_MASK 0xFF
  78. #define FADE_VAL(in, out) ((0xF & (in)) | ((0xF & (out)) << 4))
  79. #define BL_CFGR_VAL(law, blv) ((((blv) & CFGR_BLV_MASK) << CFGR_BLV_SHIFT) | ((0x3 & (law)) << 1))
  80. #define ALS_CCFG_VAL(filt) ((0x7 & filt) << 5)
  81. enum {
  82. adp8860,
  83. adp8861,
  84. adp8863
  85. };
  86. struct adp8860_led {
  87. struct led_classdev cdev;
  88. struct work_struct work;
  89. struct i2c_client *client;
  90. enum led_brightness new_brightness;
  91. int id;
  92. int flags;
  93. };
  94. struct adp8860_bl {
  95. struct i2c_client *client;
  96. struct backlight_device *bl;
  97. struct adp8860_led *led;
  98. struct adp8860_backlight_platform_data *pdata;
  99. struct mutex lock;
  100. unsigned long cached_daylight_max;
  101. int id;
  102. int revid;
  103. int current_brightness;
  104. unsigned en_ambl_sens:1;
  105. unsigned gdwn_dis:1;
  106. };
  107. static int adp8860_read(struct i2c_client *client, int reg, uint8_t *val)
  108. {
  109. int ret;
  110. ret = i2c_smbus_read_byte_data(client, reg);
  111. if (ret < 0) {
  112. dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
  113. return ret;
  114. }
  115. *val = (uint8_t)ret;
  116. return 0;
  117. }
  118. static int adp8860_write(struct i2c_client *client, u8 reg, u8 val)
  119. {
  120. return i2c_smbus_write_byte_data(client, reg, val);
  121. }
  122. static int adp8860_set_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
  123. {
  124. struct adp8860_bl *data = i2c_get_clientdata(client);
  125. uint8_t reg_val;
  126. int ret;
  127. mutex_lock(&data->lock);
  128. ret = adp8860_read(client, reg, &reg_val);
  129. if (!ret && ((reg_val & bit_mask) != bit_mask)) {
  130. reg_val |= bit_mask;
  131. ret = adp8860_write(client, reg, reg_val);
  132. }
  133. mutex_unlock(&data->lock);
  134. return ret;
  135. }
  136. static int adp8860_clr_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
  137. {
  138. struct adp8860_bl *data = i2c_get_clientdata(client);
  139. uint8_t reg_val;
  140. int ret;
  141. mutex_lock(&data->lock);
  142. ret = adp8860_read(client, reg, &reg_val);
  143. if (!ret && (reg_val & bit_mask)) {
  144. reg_val &= ~bit_mask;
  145. ret = adp8860_write(client, reg, reg_val);
  146. }
  147. mutex_unlock(&data->lock);
  148. return ret;
  149. }
  150. /*
  151. * Independent sink / LED
  152. */
  153. #if defined(ADP8860_USE_LEDS)
  154. static void adp8860_led_work(struct work_struct *work)
  155. {
  156. struct adp8860_led *led = container_of(work, struct adp8860_led, work);
  157. adp8860_write(led->client, ADP8860_ISC1 - led->id + 1,
  158. led->new_brightness >> 1);
  159. }
  160. static void adp8860_led_set(struct led_classdev *led_cdev,
  161. enum led_brightness value)
  162. {
  163. struct adp8860_led *led;
  164. led = container_of(led_cdev, struct adp8860_led, cdev);
  165. led->new_brightness = value;
  166. schedule_work(&led->work);
  167. }
  168. static int adp8860_led_setup(struct adp8860_led *led)
  169. {
  170. struct i2c_client *client = led->client;
  171. int ret = 0;
  172. ret = adp8860_write(client, ADP8860_ISC1 - led->id + 1, 0);
  173. ret |= adp8860_set_bits(client, ADP8860_ISCC, 1 << (led->id - 1));
  174. if (led->id > 4)
  175. ret |= adp8860_set_bits(client, ADP8860_ISCT1,
  176. (led->flags & 0x3) << ((led->id - 5) * 2));
  177. else
  178. ret |= adp8860_set_bits(client, ADP8860_ISCT2,
  179. (led->flags & 0x3) << ((led->id - 1) * 2));
  180. return ret;
  181. }
  182. static int __devinit adp8860_led_probe(struct i2c_client *client)
  183. {
  184. struct adp8860_backlight_platform_data *pdata =
  185. client->dev.platform_data;
  186. struct adp8860_bl *data = i2c_get_clientdata(client);
  187. struct adp8860_led *led, *led_dat;
  188. struct led_info *cur_led;
  189. int ret, i;
  190. led = kzalloc(sizeof(*led) * pdata->num_leds, GFP_KERNEL);
  191. if (led == NULL) {
  192. dev_err(&client->dev, "failed to alloc memory\n");
  193. return -ENOMEM;
  194. }
  195. ret = adp8860_write(client, ADP8860_ISCFR, pdata->led_fade_law);
  196. ret = adp8860_write(client, ADP8860_ISCT1,
  197. (pdata->led_on_time & 0x3) << 6);
  198. ret |= adp8860_write(client, ADP8860_ISCF,
  199. FADE_VAL(pdata->led_fade_in, pdata->led_fade_out));
  200. if (ret) {
  201. dev_err(&client->dev, "failed to write\n");
  202. goto err_free;
  203. }
  204. for (i = 0; i < pdata->num_leds; ++i) {
  205. cur_led = &pdata->leds[i];
  206. led_dat = &led[i];
  207. led_dat->id = cur_led->flags & ADP8860_FLAG_LED_MASK;
  208. if (led_dat->id > 7 || led_dat->id < 1) {
  209. dev_err(&client->dev, "Invalid LED ID %d\n",
  210. led_dat->id);
  211. goto err;
  212. }
  213. if (pdata->bl_led_assign & (1 << (led_dat->id - 1))) {
  214. dev_err(&client->dev, "LED %d used by Backlight\n",
  215. led_dat->id);
  216. goto err;
  217. }
  218. led_dat->cdev.name = cur_led->name;
  219. led_dat->cdev.default_trigger = cur_led->default_trigger;
  220. led_dat->cdev.brightness_set = adp8860_led_set;
  221. led_dat->cdev.brightness = LED_OFF;
  222. led_dat->flags = cur_led->flags >> FLAG_OFFT_SHIFT;
  223. led_dat->client = client;
  224. led_dat->new_brightness = LED_OFF;
  225. INIT_WORK(&led_dat->work, adp8860_led_work);
  226. ret = led_classdev_register(&client->dev, &led_dat->cdev);
  227. if (ret) {
  228. dev_err(&client->dev, "failed to register LED %d\n",
  229. led_dat->id);
  230. goto err;
  231. }
  232. ret = adp8860_led_setup(led_dat);
  233. if (ret) {
  234. dev_err(&client->dev, "failed to write\n");
  235. i++;
  236. goto err;
  237. }
  238. }
  239. data->led = led;
  240. return 0;
  241. err:
  242. for (i = i - 1; i >= 0; --i) {
  243. led_classdev_unregister(&led[i].cdev);
  244. cancel_work_sync(&led[i].work);
  245. }
  246. err_free:
  247. kfree(led);
  248. return ret;
  249. }
  250. static int __devexit adp8860_led_remove(struct i2c_client *client)
  251. {
  252. struct adp8860_backlight_platform_data *pdata =
  253. client->dev.platform_data;
  254. struct adp8860_bl *data = i2c_get_clientdata(client);
  255. int i;
  256. for (i = 0; i < pdata->num_leds; i++) {
  257. led_classdev_unregister(&data->led[i].cdev);
  258. cancel_work_sync(&data->led[i].work);
  259. }
  260. kfree(data->led);
  261. return 0;
  262. }
  263. #else
  264. static int __devinit adp8860_led_probe(struct i2c_client *client)
  265. {
  266. return 0;
  267. }
  268. static int __devexit adp8860_led_remove(struct i2c_client *client)
  269. {
  270. return 0;
  271. }
  272. #endif
  273. static int adp8860_bl_set(struct backlight_device *bl, int brightness)
  274. {
  275. struct adp8860_bl *data = bl_get_data(bl);
  276. struct i2c_client *client = data->client;
  277. int ret = 0;
  278. if (data->en_ambl_sens) {
  279. if ((brightness > 0) && (brightness < ADP8860_MAX_BRIGHTNESS)) {
  280. /* Disable Ambient Light auto adjust */
  281. ret |= adp8860_clr_bits(client, ADP8860_MDCR,
  282. CMP_AUTOEN);
  283. ret |= adp8860_write(client, ADP8860_BLMX1, brightness);
  284. } else {
  285. /*
  286. * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
  287. * restore daylight l1 sysfs brightness
  288. */
  289. ret |= adp8860_write(client, ADP8860_BLMX1,
  290. data->cached_daylight_max);
  291. ret |= adp8860_set_bits(client, ADP8860_MDCR,
  292. CMP_AUTOEN);
  293. }
  294. } else
  295. ret |= adp8860_write(client, ADP8860_BLMX1, brightness);
  296. if (data->current_brightness && brightness == 0)
  297. ret |= adp8860_set_bits(client,
  298. ADP8860_MDCR, DIM_EN);
  299. else if (data->current_brightness == 0 && brightness)
  300. ret |= adp8860_clr_bits(client,
  301. ADP8860_MDCR, DIM_EN);
  302. if (!ret)
  303. data->current_brightness = brightness;
  304. return ret;
  305. }
  306. static int adp8860_bl_update_status(struct backlight_device *bl)
  307. {
  308. int brightness = bl->props.brightness;
  309. if (bl->props.power != FB_BLANK_UNBLANK)
  310. brightness = 0;
  311. if (bl->props.fb_blank != FB_BLANK_UNBLANK)
  312. brightness = 0;
  313. return adp8860_bl_set(bl, brightness);
  314. }
  315. static int adp8860_bl_get_brightness(struct backlight_device *bl)
  316. {
  317. struct adp8860_bl *data = bl_get_data(bl);
  318. return data->current_brightness;
  319. }
  320. static const struct backlight_ops adp8860_bl_ops = {
  321. .update_status = adp8860_bl_update_status,
  322. .get_brightness = adp8860_bl_get_brightness,
  323. };
  324. static int adp8860_bl_setup(struct backlight_device *bl)
  325. {
  326. struct adp8860_bl *data = bl_get_data(bl);
  327. struct i2c_client *client = data->client;
  328. struct adp8860_backlight_platform_data *pdata = data->pdata;
  329. int ret = 0;
  330. ret |= adp8860_write(client, ADP8860_BLSEN, ~pdata->bl_led_assign);
  331. ret |= adp8860_write(client, ADP8860_BLMX1, pdata->l1_daylight_max);
  332. ret |= adp8860_write(client, ADP8860_BLDM1, pdata->l1_daylight_dim);
  333. if (data->en_ambl_sens) {
  334. data->cached_daylight_max = pdata->l1_daylight_max;
  335. ret |= adp8860_write(client, ADP8860_BLMX2,
  336. pdata->l2_office_max);
  337. ret |= adp8860_write(client, ADP8860_BLDM2,
  338. pdata->l2_office_dim);
  339. ret |= adp8860_write(client, ADP8860_BLMX3,
  340. pdata->l3_dark_max);
  341. ret |= adp8860_write(client, ADP8860_BLDM3,
  342. pdata->l3_dark_dim);
  343. ret |= adp8860_write(client, ADP8860_L2_TRP, pdata->l2_trip);
  344. ret |= adp8860_write(client, ADP8860_L2_HYS, pdata->l2_hyst);
  345. ret |= adp8860_write(client, ADP8860_L3_TRP, pdata->l3_trip);
  346. ret |= adp8860_write(client, ADP8860_L3_HYS, pdata->l3_hyst);
  347. ret |= adp8860_write(client, ADP8860_CCFG, L2_EN | L3_EN |
  348. ALS_CCFG_VAL(pdata->abml_filt));
  349. }
  350. ret |= adp8860_write(client, ADP8860_CFGR,
  351. BL_CFGR_VAL(pdata->bl_fade_law, 0));
  352. ret |= adp8860_write(client, ADP8860_BLFR, FADE_VAL(pdata->bl_fade_in,
  353. pdata->bl_fade_out));
  354. ret |= adp8860_set_bits(client, ADP8860_MDCR, BLEN | DIM_EN | NSTBY |
  355. (data->gdwn_dis ? GDWN_DIS : 0));
  356. return ret;
  357. }
  358. static ssize_t adp8860_show(struct device *dev, char *buf, int reg)
  359. {
  360. struct adp8860_bl *data = dev_get_drvdata(dev);
  361. int error;
  362. uint8_t reg_val;
  363. mutex_lock(&data->lock);
  364. error = adp8860_read(data->client, reg, &reg_val);
  365. mutex_unlock(&data->lock);
  366. if (error < 0)
  367. return error;
  368. return sprintf(buf, "%u\n", reg_val);
  369. }
  370. static ssize_t adp8860_store(struct device *dev, const char *buf,
  371. size_t count, int reg)
  372. {
  373. struct adp8860_bl *data = dev_get_drvdata(dev);
  374. unsigned long val;
  375. int ret;
  376. ret = strict_strtoul(buf, 10, &val);
  377. if (ret)
  378. return ret;
  379. mutex_lock(&data->lock);
  380. adp8860_write(data->client, reg, val);
  381. mutex_unlock(&data->lock);
  382. return count;
  383. }
  384. static ssize_t adp8860_bl_l3_dark_max_show(struct device *dev,
  385. struct device_attribute *attr, char *buf)
  386. {
  387. return adp8860_show(dev, buf, ADP8860_BLMX3);
  388. }
  389. static ssize_t adp8860_bl_l3_dark_max_store(struct device *dev,
  390. struct device_attribute *attr, const char *buf, size_t count)
  391. {
  392. return adp8860_store(dev, buf, count, ADP8860_BLMX3);
  393. }
  394. static DEVICE_ATTR(l3_dark_max, 0664, adp8860_bl_l3_dark_max_show,
  395. adp8860_bl_l3_dark_max_store);
  396. static ssize_t adp8860_bl_l2_office_max_show(struct device *dev,
  397. struct device_attribute *attr, char *buf)
  398. {
  399. return adp8860_show(dev, buf, ADP8860_BLMX2);
  400. }
  401. static ssize_t adp8860_bl_l2_office_max_store(struct device *dev,
  402. struct device_attribute *attr, const char *buf, size_t count)
  403. {
  404. return adp8860_store(dev, buf, count, ADP8860_BLMX2);
  405. }
  406. static DEVICE_ATTR(l2_office_max, 0664, adp8860_bl_l2_office_max_show,
  407. adp8860_bl_l2_office_max_store);
  408. static ssize_t adp8860_bl_l1_daylight_max_show(struct device *dev,
  409. struct device_attribute *attr, char *buf)
  410. {
  411. return adp8860_show(dev, buf, ADP8860_BLMX1);
  412. }
  413. static ssize_t adp8860_bl_l1_daylight_max_store(struct device *dev,
  414. struct device_attribute *attr, const char *buf, size_t count)
  415. {
  416. struct adp8860_bl *data = dev_get_drvdata(dev);
  417. int ret = strict_strtoul(buf, 10, &data->cached_daylight_max);
  418. if (ret)
  419. return ret;
  420. return adp8860_store(dev, buf, count, ADP8860_BLMX1);
  421. }
  422. static DEVICE_ATTR(l1_daylight_max, 0664, adp8860_bl_l1_daylight_max_show,
  423. adp8860_bl_l1_daylight_max_store);
  424. static ssize_t adp8860_bl_l3_dark_dim_show(struct device *dev,
  425. struct device_attribute *attr, char *buf)
  426. {
  427. return adp8860_show(dev, buf, ADP8860_BLDM3);
  428. }
  429. static ssize_t adp8860_bl_l3_dark_dim_store(struct device *dev,
  430. struct device_attribute *attr,
  431. const char *buf, size_t count)
  432. {
  433. return adp8860_store(dev, buf, count, ADP8860_BLDM3);
  434. }
  435. static DEVICE_ATTR(l3_dark_dim, 0664, adp8860_bl_l3_dark_dim_show,
  436. adp8860_bl_l3_dark_dim_store);
  437. static ssize_t adp8860_bl_l2_office_dim_show(struct device *dev,
  438. struct device_attribute *attr, char *buf)
  439. {
  440. return adp8860_show(dev, buf, ADP8860_BLDM2);
  441. }
  442. static ssize_t adp8860_bl_l2_office_dim_store(struct device *dev,
  443. struct device_attribute *attr,
  444. const char *buf, size_t count)
  445. {
  446. return adp8860_store(dev, buf, count, ADP8860_BLDM2);
  447. }
  448. static DEVICE_ATTR(l2_office_dim, 0664, adp8860_bl_l2_office_dim_show,
  449. adp8860_bl_l2_office_dim_store);
  450. static ssize_t adp8860_bl_l1_daylight_dim_show(struct device *dev,
  451. struct device_attribute *attr, char *buf)
  452. {
  453. return adp8860_show(dev, buf, ADP8860_BLDM1);
  454. }
  455. static ssize_t adp8860_bl_l1_daylight_dim_store(struct device *dev,
  456. struct device_attribute *attr,
  457. const char *buf, size_t count)
  458. {
  459. return adp8860_store(dev, buf, count, ADP8860_BLDM1);
  460. }
  461. static DEVICE_ATTR(l1_daylight_dim, 0664, adp8860_bl_l1_daylight_dim_show,
  462. adp8860_bl_l1_daylight_dim_store);
  463. #ifdef ADP8860_EXT_FEATURES
  464. static ssize_t adp8860_bl_ambient_light_level_show(struct device *dev,
  465. struct device_attribute *attr, char *buf)
  466. {
  467. struct adp8860_bl *data = dev_get_drvdata(dev);
  468. int error;
  469. uint8_t reg_val;
  470. uint16_t ret_val;
  471. mutex_lock(&data->lock);
  472. error = adp8860_read(data->client, ADP8860_PH1LEVL, &reg_val);
  473. ret_val = reg_val;
  474. error |= adp8860_read(data->client, ADP8860_PH1LEVH, &reg_val);
  475. mutex_unlock(&data->lock);
  476. if (error < 0)
  477. return error;
  478. /* Return 13-bit conversion value for the first light sensor */
  479. ret_val += (reg_val & 0x1F) << 8;
  480. return sprintf(buf, "%u\n", ret_val);
  481. }
  482. static DEVICE_ATTR(ambient_light_level, 0444,
  483. adp8860_bl_ambient_light_level_show, NULL);
  484. static ssize_t adp8860_bl_ambient_light_zone_show(struct device *dev,
  485. struct device_attribute *attr, char *buf)
  486. {
  487. struct adp8860_bl *data = dev_get_drvdata(dev);
  488. int error;
  489. uint8_t reg_val;
  490. mutex_lock(&data->lock);
  491. error = adp8860_read(data->client, ADP8860_CFGR, &reg_val);
  492. mutex_unlock(&data->lock);
  493. if (error < 0)
  494. return error;
  495. return sprintf(buf, "%u\n",
  496. ((reg_val >> CFGR_BLV_SHIFT) & CFGR_BLV_MASK) + 1);
  497. }
  498. static ssize_t adp8860_bl_ambient_light_zone_store(struct device *dev,
  499. struct device_attribute *attr,
  500. const char *buf, size_t count)
  501. {
  502. struct adp8860_bl *data = dev_get_drvdata(dev);
  503. unsigned long val;
  504. uint8_t reg_val;
  505. int ret;
  506. ret = strict_strtoul(buf, 10, &val);
  507. if (ret)
  508. return ret;
  509. if (val == 0) {
  510. /* Enable automatic ambient light sensing */
  511. adp8860_set_bits(data->client, ADP8860_MDCR, CMP_AUTOEN);
  512. } else if ((val > 0) && (val <= 3)) {
  513. /* Disable automatic ambient light sensing */
  514. adp8860_clr_bits(data->client, ADP8860_MDCR, CMP_AUTOEN);
  515. /* Set user supplied ambient light zone */
  516. mutex_lock(&data->lock);
  517. adp8860_read(data->client, ADP8860_CFGR, &reg_val);
  518. reg_val &= ~(CFGR_BLV_MASK << CFGR_BLV_SHIFT);
  519. reg_val |= (val - 1) << CFGR_BLV_SHIFT;
  520. adp8860_write(data->client, ADP8860_CFGR, reg_val);
  521. mutex_unlock(&data->lock);
  522. }
  523. return count;
  524. }
  525. static DEVICE_ATTR(ambient_light_zone, 0664,
  526. adp8860_bl_ambient_light_zone_show,
  527. adp8860_bl_ambient_light_zone_store);
  528. #endif
  529. static struct attribute *adp8860_bl_attributes[] = {
  530. &dev_attr_l3_dark_max.attr,
  531. &dev_attr_l3_dark_dim.attr,
  532. &dev_attr_l2_office_max.attr,
  533. &dev_attr_l2_office_dim.attr,
  534. &dev_attr_l1_daylight_max.attr,
  535. &dev_attr_l1_daylight_dim.attr,
  536. #ifdef ADP8860_EXT_FEATURES
  537. &dev_attr_ambient_light_level.attr,
  538. &dev_attr_ambient_light_zone.attr,
  539. #endif
  540. NULL
  541. };
  542. static const struct attribute_group adp8860_bl_attr_group = {
  543. .attrs = adp8860_bl_attributes,
  544. };
  545. static int __devinit adp8860_probe(struct i2c_client *client,
  546. const struct i2c_device_id *id)
  547. {
  548. struct backlight_device *bl;
  549. struct adp8860_bl *data;
  550. struct adp8860_backlight_platform_data *pdata =
  551. client->dev.platform_data;
  552. struct backlight_properties props;
  553. uint8_t reg_val;
  554. int ret;
  555. if (!i2c_check_functionality(client->adapter,
  556. I2C_FUNC_SMBUS_BYTE_DATA)) {
  557. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  558. return -EIO;
  559. }
  560. if (!pdata) {
  561. dev_err(&client->dev, "no platform data?\n");
  562. return -EINVAL;
  563. }
  564. data = kzalloc(sizeof(*data), GFP_KERNEL);
  565. if (data == NULL)
  566. return -ENOMEM;
  567. ret = adp8860_read(client, ADP8860_MFDVID, &reg_val);
  568. if (ret < 0)
  569. goto out2;
  570. switch (ADP8860_MANID(reg_val)) {
  571. case ADP8863_MANUFID:
  572. data->gdwn_dis = !!pdata->gdwn_dis;
  573. case ADP8860_MANUFID:
  574. data->en_ambl_sens = !!pdata->en_ambl_sens;
  575. break;
  576. case ADP8861_MANUFID:
  577. data->gdwn_dis = !!pdata->gdwn_dis;
  578. break;
  579. default:
  580. dev_err(&client->dev, "failed to probe\n");
  581. ret = -ENODEV;
  582. goto out2;
  583. }
  584. /* It's confirmed that the DEVID field is actually a REVID */
  585. data->revid = ADP8860_DEVID(reg_val);
  586. data->client = client;
  587. data->pdata = pdata;
  588. data->id = id->driver_data;
  589. data->current_brightness = 0;
  590. i2c_set_clientdata(client, data);
  591. memset(&props, 0, sizeof(props));
  592. props.type = BACKLIGHT_RAW;
  593. props.max_brightness = ADP8860_MAX_BRIGHTNESS;
  594. mutex_init(&data->lock);
  595. bl = backlight_device_register(dev_driver_string(&client->dev),
  596. &client->dev, data, &adp8860_bl_ops, &props);
  597. if (IS_ERR(bl)) {
  598. dev_err(&client->dev, "failed to register backlight\n");
  599. ret = PTR_ERR(bl);
  600. goto out2;
  601. }
  602. bl->props.brightness = ADP8860_MAX_BRIGHTNESS;
  603. data->bl = bl;
  604. if (data->en_ambl_sens)
  605. ret = sysfs_create_group(&bl->dev.kobj,
  606. &adp8860_bl_attr_group);
  607. if (ret) {
  608. dev_err(&client->dev, "failed to register sysfs\n");
  609. goto out1;
  610. }
  611. ret = adp8860_bl_setup(bl);
  612. if (ret) {
  613. ret = -EIO;
  614. goto out;
  615. }
  616. backlight_update_status(bl);
  617. dev_info(&client->dev, "%s Rev.%d Backlight\n",
  618. client->name, data->revid);
  619. if (pdata->num_leds)
  620. adp8860_led_probe(client);
  621. return 0;
  622. out:
  623. if (data->en_ambl_sens)
  624. sysfs_remove_group(&data->bl->dev.kobj,
  625. &adp8860_bl_attr_group);
  626. out1:
  627. backlight_device_unregister(bl);
  628. out2:
  629. kfree(data);
  630. return ret;
  631. }
  632. static int __devexit adp8860_remove(struct i2c_client *client)
  633. {
  634. struct adp8860_bl *data = i2c_get_clientdata(client);
  635. adp8860_clr_bits(client, ADP8860_MDCR, NSTBY);
  636. if (data->led)
  637. adp8860_led_remove(client);
  638. if (data->en_ambl_sens)
  639. sysfs_remove_group(&data->bl->dev.kobj,
  640. &adp8860_bl_attr_group);
  641. backlight_device_unregister(data->bl);
  642. kfree(data);
  643. return 0;
  644. }
  645. #ifdef CONFIG_PM
  646. static int adp8860_i2c_suspend(struct i2c_client *client, pm_message_t message)
  647. {
  648. adp8860_clr_bits(client, ADP8860_MDCR, NSTBY);
  649. return 0;
  650. }
  651. static int adp8860_i2c_resume(struct i2c_client *client)
  652. {
  653. adp8860_set_bits(client, ADP8860_MDCR, NSTBY);
  654. return 0;
  655. }
  656. #else
  657. #define adp8860_i2c_suspend NULL
  658. #define adp8860_i2c_resume NULL
  659. #endif
  660. static const struct i2c_device_id adp8860_id[] = {
  661. { "adp8860", adp8860 },
  662. { "adp8861", adp8861 },
  663. { "adp8863", adp8863 },
  664. { }
  665. };
  666. MODULE_DEVICE_TABLE(i2c, adp8860_id);
  667. static struct i2c_driver adp8860_driver = {
  668. .driver = {
  669. .name = KBUILD_MODNAME,
  670. },
  671. .probe = adp8860_probe,
  672. .remove = __devexit_p(adp8860_remove),
  673. .suspend = adp8860_i2c_suspend,
  674. .resume = adp8860_i2c_resume,
  675. .id_table = adp8860_id,
  676. };
  677. module_i2c_driver(adp8860_driver);
  678. MODULE_LICENSE("GPL v2");
  679. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  680. MODULE_DESCRIPTION("ADP8860 Backlight driver");
  681. MODULE_ALIAS("i2c:adp8860-backlight");