adp8860_bl.c 22 KB

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