adp8860_bl.c 22 KB

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