adp8860_bl.c 22 KB

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