adp8860_bl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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 adp8860_led_probe(struct i2c_client *client)
  183. {
  184. struct adp8860_backlight_platform_data *pdata =
  185. dev_get_platdata(&client->dev);
  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 = devm_kzalloc(&client->dev, sizeof(*led) * pdata->num_leds,
  191. GFP_KERNEL);
  192. if (led == NULL)
  193. return -ENOMEM;
  194. ret = adp8860_write(client, ADP8860_ISCFR, pdata->led_fade_law);
  195. ret = adp8860_write(client, ADP8860_ISCT1,
  196. (pdata->led_on_time & 0x3) << 6);
  197. ret |= adp8860_write(client, ADP8860_ISCF,
  198. FADE_VAL(pdata->led_fade_in, pdata->led_fade_out));
  199. if (ret) {
  200. dev_err(&client->dev, "failed to write\n");
  201. return ret;
  202. }
  203. for (i = 0; i < pdata->num_leds; ++i) {
  204. cur_led = &pdata->leds[i];
  205. led_dat = &led[i];
  206. led_dat->id = cur_led->flags & ADP8860_FLAG_LED_MASK;
  207. if (led_dat->id > 7 || led_dat->id < 1) {
  208. dev_err(&client->dev, "Invalid LED ID %d\n",
  209. led_dat->id);
  210. ret = -EINVAL;
  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. ret = -EBUSY;
  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. return ret;
  248. }
  249. static int adp8860_led_remove(struct i2c_client *client)
  250. {
  251. struct adp8860_backlight_platform_data *pdata =
  252. dev_get_platdata(&client->dev);
  253. struct adp8860_bl *data = i2c_get_clientdata(client);
  254. int i;
  255. for (i = 0; i < pdata->num_leds; i++) {
  256. led_classdev_unregister(&data->led[i].cdev);
  257. cancel_work_sync(&data->led[i].work);
  258. }
  259. return 0;
  260. }
  261. #else
  262. static int adp8860_led_probe(struct i2c_client *client)
  263. {
  264. return 0;
  265. }
  266. static int adp8860_led_remove(struct i2c_client *client)
  267. {
  268. return 0;
  269. }
  270. #endif
  271. static int adp8860_bl_set(struct backlight_device *bl, int brightness)
  272. {
  273. struct adp8860_bl *data = bl_get_data(bl);
  274. struct i2c_client *client = data->client;
  275. int ret = 0;
  276. if (data->en_ambl_sens) {
  277. if ((brightness > 0) && (brightness < ADP8860_MAX_BRIGHTNESS)) {
  278. /* Disable Ambient Light auto adjust */
  279. ret |= adp8860_clr_bits(client, ADP8860_MDCR,
  280. CMP_AUTOEN);
  281. ret |= adp8860_write(client, ADP8860_BLMX1, brightness);
  282. } else {
  283. /*
  284. * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
  285. * restore daylight l1 sysfs brightness
  286. */
  287. ret |= adp8860_write(client, ADP8860_BLMX1,
  288. data->cached_daylight_max);
  289. ret |= adp8860_set_bits(client, ADP8860_MDCR,
  290. CMP_AUTOEN);
  291. }
  292. } else
  293. ret |= adp8860_write(client, ADP8860_BLMX1, brightness);
  294. if (data->current_brightness && brightness == 0)
  295. ret |= adp8860_set_bits(client,
  296. ADP8860_MDCR, DIM_EN);
  297. else if (data->current_brightness == 0 && brightness)
  298. ret |= adp8860_clr_bits(client,
  299. ADP8860_MDCR, DIM_EN);
  300. if (!ret)
  301. data->current_brightness = brightness;
  302. return ret;
  303. }
  304. static int adp8860_bl_update_status(struct backlight_device *bl)
  305. {
  306. int brightness = bl->props.brightness;
  307. if (bl->props.power != FB_BLANK_UNBLANK)
  308. brightness = 0;
  309. if (bl->props.fb_blank != FB_BLANK_UNBLANK)
  310. brightness = 0;
  311. return adp8860_bl_set(bl, brightness);
  312. }
  313. static int adp8860_bl_get_brightness(struct backlight_device *bl)
  314. {
  315. struct adp8860_bl *data = bl_get_data(bl);
  316. return data->current_brightness;
  317. }
  318. static const struct backlight_ops adp8860_bl_ops = {
  319. .update_status = adp8860_bl_update_status,
  320. .get_brightness = adp8860_bl_get_brightness,
  321. };
  322. static int adp8860_bl_setup(struct backlight_device *bl)
  323. {
  324. struct adp8860_bl *data = bl_get_data(bl);
  325. struct i2c_client *client = data->client;
  326. struct adp8860_backlight_platform_data *pdata = data->pdata;
  327. int ret = 0;
  328. ret |= adp8860_write(client, ADP8860_BLSEN, ~pdata->bl_led_assign);
  329. ret |= adp8860_write(client, ADP8860_BLMX1, pdata->l1_daylight_max);
  330. ret |= adp8860_write(client, ADP8860_BLDM1, pdata->l1_daylight_dim);
  331. if (data->en_ambl_sens) {
  332. data->cached_daylight_max = pdata->l1_daylight_max;
  333. ret |= adp8860_write(client, ADP8860_BLMX2,
  334. pdata->l2_office_max);
  335. ret |= adp8860_write(client, ADP8860_BLDM2,
  336. pdata->l2_office_dim);
  337. ret |= adp8860_write(client, ADP8860_BLMX3,
  338. pdata->l3_dark_max);
  339. ret |= adp8860_write(client, ADP8860_BLDM3,
  340. pdata->l3_dark_dim);
  341. ret |= adp8860_write(client, ADP8860_L2_TRP, pdata->l2_trip);
  342. ret |= adp8860_write(client, ADP8860_L2_HYS, pdata->l2_hyst);
  343. ret |= adp8860_write(client, ADP8860_L3_TRP, pdata->l3_trip);
  344. ret |= adp8860_write(client, ADP8860_L3_HYS, pdata->l3_hyst);
  345. ret |= adp8860_write(client, ADP8860_CCFG, L2_EN | L3_EN |
  346. ALS_CCFG_VAL(pdata->abml_filt));
  347. }
  348. ret |= adp8860_write(client, ADP8860_CFGR,
  349. BL_CFGR_VAL(pdata->bl_fade_law, 0));
  350. ret |= adp8860_write(client, ADP8860_BLFR, FADE_VAL(pdata->bl_fade_in,
  351. pdata->bl_fade_out));
  352. ret |= adp8860_set_bits(client, ADP8860_MDCR, BLEN | DIM_EN | NSTBY |
  353. (data->gdwn_dis ? GDWN_DIS : 0));
  354. return ret;
  355. }
  356. static ssize_t adp8860_show(struct device *dev, char *buf, int reg)
  357. {
  358. struct adp8860_bl *data = dev_get_drvdata(dev);
  359. int error;
  360. uint8_t reg_val;
  361. mutex_lock(&data->lock);
  362. error = adp8860_read(data->client, reg, &reg_val);
  363. mutex_unlock(&data->lock);
  364. if (error < 0)
  365. return error;
  366. return sprintf(buf, "%u\n", reg_val);
  367. }
  368. static ssize_t adp8860_store(struct device *dev, const char *buf,
  369. size_t count, int reg)
  370. {
  371. struct adp8860_bl *data = dev_get_drvdata(dev);
  372. unsigned long val;
  373. int ret;
  374. ret = kstrtoul(buf, 10, &val);
  375. if (ret)
  376. return ret;
  377. mutex_lock(&data->lock);
  378. adp8860_write(data->client, reg, val);
  379. mutex_unlock(&data->lock);
  380. return count;
  381. }
  382. static ssize_t adp8860_bl_l3_dark_max_show(struct device *dev,
  383. struct device_attribute *attr, char *buf)
  384. {
  385. return adp8860_show(dev, buf, ADP8860_BLMX3);
  386. }
  387. static ssize_t adp8860_bl_l3_dark_max_store(struct device *dev,
  388. struct device_attribute *attr, const char *buf, size_t count)
  389. {
  390. return adp8860_store(dev, buf, count, ADP8860_BLMX3);
  391. }
  392. static DEVICE_ATTR(l3_dark_max, 0664, adp8860_bl_l3_dark_max_show,
  393. adp8860_bl_l3_dark_max_store);
  394. static ssize_t adp8860_bl_l2_office_max_show(struct device *dev,
  395. struct device_attribute *attr, char *buf)
  396. {
  397. return adp8860_show(dev, buf, ADP8860_BLMX2);
  398. }
  399. static ssize_t adp8860_bl_l2_office_max_store(struct device *dev,
  400. struct device_attribute *attr, const char *buf, size_t count)
  401. {
  402. return adp8860_store(dev, buf, count, ADP8860_BLMX2);
  403. }
  404. static DEVICE_ATTR(l2_office_max, 0664, adp8860_bl_l2_office_max_show,
  405. adp8860_bl_l2_office_max_store);
  406. static ssize_t adp8860_bl_l1_daylight_max_show(struct device *dev,
  407. struct device_attribute *attr, char *buf)
  408. {
  409. return adp8860_show(dev, buf, ADP8860_BLMX1);
  410. }
  411. static ssize_t adp8860_bl_l1_daylight_max_store(struct device *dev,
  412. struct device_attribute *attr, const char *buf, size_t count)
  413. {
  414. struct adp8860_bl *data = dev_get_drvdata(dev);
  415. int ret = kstrtoul(buf, 10, &data->cached_daylight_max);
  416. if (ret)
  417. return ret;
  418. return adp8860_store(dev, buf, count, ADP8860_BLMX1);
  419. }
  420. static DEVICE_ATTR(l1_daylight_max, 0664, adp8860_bl_l1_daylight_max_show,
  421. adp8860_bl_l1_daylight_max_store);
  422. static ssize_t adp8860_bl_l3_dark_dim_show(struct device *dev,
  423. struct device_attribute *attr, char *buf)
  424. {
  425. return adp8860_show(dev, buf, ADP8860_BLDM3);
  426. }
  427. static ssize_t adp8860_bl_l3_dark_dim_store(struct device *dev,
  428. struct device_attribute *attr,
  429. const char *buf, size_t count)
  430. {
  431. return adp8860_store(dev, buf, count, ADP8860_BLDM3);
  432. }
  433. static DEVICE_ATTR(l3_dark_dim, 0664, adp8860_bl_l3_dark_dim_show,
  434. adp8860_bl_l3_dark_dim_store);
  435. static ssize_t adp8860_bl_l2_office_dim_show(struct device *dev,
  436. struct device_attribute *attr, char *buf)
  437. {
  438. return adp8860_show(dev, buf, ADP8860_BLDM2);
  439. }
  440. static ssize_t adp8860_bl_l2_office_dim_store(struct device *dev,
  441. struct device_attribute *attr,
  442. const char *buf, size_t count)
  443. {
  444. return adp8860_store(dev, buf, count, ADP8860_BLDM2);
  445. }
  446. static DEVICE_ATTR(l2_office_dim, 0664, adp8860_bl_l2_office_dim_show,
  447. adp8860_bl_l2_office_dim_store);
  448. static ssize_t adp8860_bl_l1_daylight_dim_show(struct device *dev,
  449. struct device_attribute *attr, char *buf)
  450. {
  451. return adp8860_show(dev, buf, ADP8860_BLDM1);
  452. }
  453. static ssize_t adp8860_bl_l1_daylight_dim_store(struct device *dev,
  454. struct device_attribute *attr,
  455. const char *buf, size_t count)
  456. {
  457. return adp8860_store(dev, buf, count, ADP8860_BLDM1);
  458. }
  459. static DEVICE_ATTR(l1_daylight_dim, 0664, adp8860_bl_l1_daylight_dim_show,
  460. adp8860_bl_l1_daylight_dim_store);
  461. #ifdef ADP8860_EXT_FEATURES
  462. static ssize_t adp8860_bl_ambient_light_level_show(struct device *dev,
  463. struct device_attribute *attr, char *buf)
  464. {
  465. struct adp8860_bl *data = dev_get_drvdata(dev);
  466. int error;
  467. uint8_t reg_val;
  468. uint16_t ret_val;
  469. mutex_lock(&data->lock);
  470. error = adp8860_read(data->client, ADP8860_PH1LEVL, &reg_val);
  471. ret_val = reg_val;
  472. error |= adp8860_read(data->client, ADP8860_PH1LEVH, &reg_val);
  473. mutex_unlock(&data->lock);
  474. if (error < 0)
  475. return error;
  476. /* Return 13-bit conversion value for the first light sensor */
  477. ret_val += (reg_val & 0x1F) << 8;
  478. return sprintf(buf, "%u\n", ret_val);
  479. }
  480. static DEVICE_ATTR(ambient_light_level, 0444,
  481. adp8860_bl_ambient_light_level_show, NULL);
  482. static ssize_t adp8860_bl_ambient_light_zone_show(struct device *dev,
  483. struct device_attribute *attr, char *buf)
  484. {
  485. struct adp8860_bl *data = dev_get_drvdata(dev);
  486. int error;
  487. uint8_t reg_val;
  488. mutex_lock(&data->lock);
  489. error = adp8860_read(data->client, ADP8860_CFGR, &reg_val);
  490. mutex_unlock(&data->lock);
  491. if (error < 0)
  492. return error;
  493. return sprintf(buf, "%u\n",
  494. ((reg_val >> CFGR_BLV_SHIFT) & CFGR_BLV_MASK) + 1);
  495. }
  496. static ssize_t adp8860_bl_ambient_light_zone_store(struct device *dev,
  497. struct device_attribute *attr,
  498. const char *buf, size_t count)
  499. {
  500. struct adp8860_bl *data = dev_get_drvdata(dev);
  501. unsigned long val;
  502. uint8_t reg_val;
  503. int ret;
  504. ret = kstrtoul(buf, 10, &val);
  505. if (ret)
  506. return ret;
  507. if (val == 0) {
  508. /* Enable automatic ambient light sensing */
  509. adp8860_set_bits(data->client, ADP8860_MDCR, CMP_AUTOEN);
  510. } else if ((val > 0) && (val <= 3)) {
  511. /* Disable automatic ambient light sensing */
  512. adp8860_clr_bits(data->client, ADP8860_MDCR, CMP_AUTOEN);
  513. /* Set user supplied ambient light zone */
  514. mutex_lock(&data->lock);
  515. adp8860_read(data->client, ADP8860_CFGR, &reg_val);
  516. reg_val &= ~(CFGR_BLV_MASK << CFGR_BLV_SHIFT);
  517. reg_val |= (val - 1) << CFGR_BLV_SHIFT;
  518. adp8860_write(data->client, ADP8860_CFGR, reg_val);
  519. mutex_unlock(&data->lock);
  520. }
  521. return count;
  522. }
  523. static DEVICE_ATTR(ambient_light_zone, 0664,
  524. adp8860_bl_ambient_light_zone_show,
  525. adp8860_bl_ambient_light_zone_store);
  526. #endif
  527. static struct attribute *adp8860_bl_attributes[] = {
  528. &dev_attr_l3_dark_max.attr,
  529. &dev_attr_l3_dark_dim.attr,
  530. &dev_attr_l2_office_max.attr,
  531. &dev_attr_l2_office_dim.attr,
  532. &dev_attr_l1_daylight_max.attr,
  533. &dev_attr_l1_daylight_dim.attr,
  534. #ifdef ADP8860_EXT_FEATURES
  535. &dev_attr_ambient_light_level.attr,
  536. &dev_attr_ambient_light_zone.attr,
  537. #endif
  538. NULL
  539. };
  540. static const struct attribute_group adp8860_bl_attr_group = {
  541. .attrs = adp8860_bl_attributes,
  542. };
  543. static int adp8860_probe(struct i2c_client *client,
  544. const struct i2c_device_id *id)
  545. {
  546. struct backlight_device *bl;
  547. struct adp8860_bl *data;
  548. struct adp8860_backlight_platform_data *pdata =
  549. dev_get_platdata(&client->dev);
  550. struct backlight_properties props;
  551. uint8_t reg_val;
  552. int ret;
  553. if (!i2c_check_functionality(client->adapter,
  554. I2C_FUNC_SMBUS_BYTE_DATA)) {
  555. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  556. return -EIO;
  557. }
  558. if (!pdata) {
  559. dev_err(&client->dev, "no platform data?\n");
  560. return -EINVAL;
  561. }
  562. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  563. if (data == NULL)
  564. return -ENOMEM;
  565. ret = adp8860_read(client, ADP8860_MFDVID, &reg_val);
  566. if (ret < 0)
  567. return ret;
  568. switch (ADP8860_MANID(reg_val)) {
  569. case ADP8863_MANUFID:
  570. data->gdwn_dis = !!pdata->gdwn_dis;
  571. case ADP8860_MANUFID:
  572. data->en_ambl_sens = !!pdata->en_ambl_sens;
  573. break;
  574. case ADP8861_MANUFID:
  575. data->gdwn_dis = !!pdata->gdwn_dis;
  576. break;
  577. default:
  578. dev_err(&client->dev, "failed to probe\n");
  579. return -ENODEV;
  580. }
  581. /* It's confirmed that the DEVID field is actually a REVID */
  582. data->revid = ADP8860_DEVID(reg_val);
  583. data->client = client;
  584. data->pdata = pdata;
  585. data->id = id->driver_data;
  586. data->current_brightness = 0;
  587. i2c_set_clientdata(client, data);
  588. memset(&props, 0, sizeof(props));
  589. props.type = BACKLIGHT_RAW;
  590. props.max_brightness = ADP8860_MAX_BRIGHTNESS;
  591. mutex_init(&data->lock);
  592. bl = devm_backlight_device_register(&client->dev,
  593. dev_driver_string(&client->dev),
  594. &client->dev, data, &adp8860_bl_ops, &props);
  595. if (IS_ERR(bl)) {
  596. dev_err(&client->dev, "failed to register backlight\n");
  597. return PTR_ERR(bl);
  598. }
  599. bl->props.brightness = ADP8860_MAX_BRIGHTNESS;
  600. data->bl = bl;
  601. if (data->en_ambl_sens)
  602. ret = sysfs_create_group(&bl->dev.kobj,
  603. &adp8860_bl_attr_group);
  604. if (ret) {
  605. dev_err(&client->dev, "failed to register sysfs\n");
  606. return ret;
  607. }
  608. ret = adp8860_bl_setup(bl);
  609. if (ret) {
  610. ret = -EIO;
  611. goto out;
  612. }
  613. backlight_update_status(bl);
  614. dev_info(&client->dev, "%s Rev.%d Backlight\n",
  615. client->name, data->revid);
  616. if (pdata->num_leds)
  617. adp8860_led_probe(client);
  618. return 0;
  619. out:
  620. if (data->en_ambl_sens)
  621. sysfs_remove_group(&data->bl->dev.kobj,
  622. &adp8860_bl_attr_group);
  623. return ret;
  624. }
  625. static int adp8860_remove(struct i2c_client *client)
  626. {
  627. struct adp8860_bl *data = i2c_get_clientdata(client);
  628. adp8860_clr_bits(client, ADP8860_MDCR, NSTBY);
  629. if (data->led)
  630. adp8860_led_remove(client);
  631. if (data->en_ambl_sens)
  632. sysfs_remove_group(&data->bl->dev.kobj,
  633. &adp8860_bl_attr_group);
  634. return 0;
  635. }
  636. #ifdef CONFIG_PM_SLEEP
  637. static int adp8860_i2c_suspend(struct device *dev)
  638. {
  639. struct i2c_client *client = to_i2c_client(dev);
  640. adp8860_clr_bits(client, ADP8860_MDCR, NSTBY);
  641. return 0;
  642. }
  643. static int adp8860_i2c_resume(struct device *dev)
  644. {
  645. struct i2c_client *client = to_i2c_client(dev);
  646. adp8860_set_bits(client, ADP8860_MDCR, NSTBY | BLEN);
  647. return 0;
  648. }
  649. #endif
  650. static SIMPLE_DEV_PM_OPS(adp8860_i2c_pm_ops, adp8860_i2c_suspend,
  651. adp8860_i2c_resume);
  652. static const struct i2c_device_id adp8860_id[] = {
  653. { "adp8860", adp8860 },
  654. { "adp8861", adp8861 },
  655. { "adp8863", adp8863 },
  656. { }
  657. };
  658. MODULE_DEVICE_TABLE(i2c, adp8860_id);
  659. static struct i2c_driver adp8860_driver = {
  660. .driver = {
  661. .name = KBUILD_MODNAME,
  662. .pm = &adp8860_i2c_pm_ops,
  663. },
  664. .probe = adp8860_probe,
  665. .remove = adp8860_remove,
  666. .id_table = adp8860_id,
  667. };
  668. module_i2c_driver(adp8860_driver);
  669. MODULE_LICENSE("GPL v2");
  670. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  671. MODULE_DESCRIPTION("ADP8860 Backlight driver");