adp8870_bl.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Backlight driver for Analog Devices ADP8870 Backlight Devices
  4. *
  5. * Copyright 2009-2011 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/workqueue.h>
  17. #include <linux/slab.h>
  18. #include <linux/platform_data/adp8870.h>
  19. #define ADP8870_EXT_FEATURES
  20. #define ADP8870_USE_LEDS
  21. #define ADP8870_MFDVID 0x00 /* Manufacturer and device ID */
  22. #define ADP8870_MDCR 0x01 /* Device mode and status */
  23. #define ADP8870_INT_STAT 0x02 /* Interrupts status */
  24. #define ADP8870_INT_EN 0x03 /* Interrupts enable */
  25. #define ADP8870_CFGR 0x04 /* Configuration register */
  26. #define ADP8870_BLSEL 0x05 /* Sink enable backlight or independent */
  27. #define ADP8870_PWMLED 0x06 /* PWM Enable Selection Register */
  28. #define ADP8870_BLOFF 0x07 /* Backlight off timeout */
  29. #define ADP8870_BLDIM 0x08 /* Backlight dim timeout */
  30. #define ADP8870_BLFR 0x09 /* Backlight fade in and out rates */
  31. #define ADP8870_BLMX1 0x0A /* Backlight (Brightness Level 1-daylight) maximum current */
  32. #define ADP8870_BLDM1 0x0B /* Backlight (Brightness Level 1-daylight) dim current */
  33. #define ADP8870_BLMX2 0x0C /* Backlight (Brightness Level 2-bright) maximum current */
  34. #define ADP8870_BLDM2 0x0D /* Backlight (Brightness Level 2-bright) dim current */
  35. #define ADP8870_BLMX3 0x0E /* Backlight (Brightness Level 3-office) maximum current */
  36. #define ADP8870_BLDM3 0x0F /* Backlight (Brightness Level 3-office) dim current */
  37. #define ADP8870_BLMX4 0x10 /* Backlight (Brightness Level 4-indoor) maximum current */
  38. #define ADP8870_BLDM4 0x11 /* Backlight (Brightness Level 4-indoor) dim current */
  39. #define ADP8870_BLMX5 0x12 /* Backlight (Brightness Level 5-dark) maximum current */
  40. #define ADP8870_BLDM5 0x13 /* Backlight (Brightness Level 5-dark) dim current */
  41. #define ADP8870_ISCLAW 0x1A /* Independent sink current fade law register */
  42. #define ADP8870_ISCC 0x1B /* Independent sink current control register */
  43. #define ADP8870_ISCT1 0x1C /* Independent Sink Current Timer Register LED[7:5] */
  44. #define ADP8870_ISCT2 0x1D /* Independent Sink Current Timer Register LED[4:1] */
  45. #define ADP8870_ISCF 0x1E /* Independent sink current fade register */
  46. #define ADP8870_ISC1 0x1F /* Independent Sink Current LED1 */
  47. #define ADP8870_ISC2 0x20 /* Independent Sink Current LED2 */
  48. #define ADP8870_ISC3 0x21 /* Independent Sink Current LED3 */
  49. #define ADP8870_ISC4 0x22 /* Independent Sink Current LED4 */
  50. #define ADP8870_ISC5 0x23 /* Independent Sink Current LED5 */
  51. #define ADP8870_ISC6 0x24 /* Independent Sink Current LED6 */
  52. #define ADP8870_ISC7 0x25 /* Independent Sink Current LED7 (Brightness Level 1-daylight) */
  53. #define ADP8870_ISC7_L2 0x26 /* Independent Sink Current LED7 (Brightness Level 2-bright) */
  54. #define ADP8870_ISC7_L3 0x27 /* Independent Sink Current LED7 (Brightness Level 3-office) */
  55. #define ADP8870_ISC7_L4 0x28 /* Independent Sink Current LED7 (Brightness Level 4-indoor) */
  56. #define ADP8870_ISC7_L5 0x29 /* Independent Sink Current LED7 (Brightness Level 5-dark) */
  57. #define ADP8870_CMP_CTL 0x2D /* ALS Comparator Control Register */
  58. #define ADP8870_ALS1_EN 0x2E /* Main ALS comparator level enable */
  59. #define ADP8870_ALS2_EN 0x2F /* Second ALS comparator level enable */
  60. #define ADP8870_ALS1_STAT 0x30 /* Main ALS Comparator Status Register */
  61. #define ADP8870_ALS2_STAT 0x31 /* Second ALS Comparator Status Register */
  62. #define ADP8870_L2TRP 0x32 /* L2 comparator reference */
  63. #define ADP8870_L2HYS 0x33 /* L2 hysteresis */
  64. #define ADP8870_L3TRP 0x34 /* L3 comparator reference */
  65. #define ADP8870_L3HYS 0x35 /* L3 hysteresis */
  66. #define ADP8870_L4TRP 0x36 /* L4 comparator reference */
  67. #define ADP8870_L4HYS 0x37 /* L4 hysteresis */
  68. #define ADP8870_L5TRP 0x38 /* L5 comparator reference */
  69. #define ADP8870_L5HYS 0x39 /* L5 hysteresis */
  70. #define ADP8870_PH1LEVL 0x40 /* First phototransistor ambient light level-low byte register */
  71. #define ADP8870_PH1LEVH 0x41 /* First phototransistor ambient light level-high byte register */
  72. #define ADP8870_PH2LEVL 0x42 /* Second phototransistor ambient light level-low byte register */
  73. #define ADP8870_PH2LEVH 0x43 /* Second phototransistor ambient light level-high byte register */
  74. #define ADP8870_MANUFID 0x3 /* Analog Devices AD8870 Manufacturer and device ID */
  75. #define ADP8870_DEVID(x) ((x) & 0xF)
  76. #define ADP8870_MANID(x) ((x) >> 4)
  77. /* MDCR Device mode and status */
  78. #define D7ALSEN (1 << 7)
  79. #define INT_CFG (1 << 6)
  80. #define NSTBY (1 << 5)
  81. #define DIM_EN (1 << 4)
  82. #define GDWN_DIS (1 << 3)
  83. #define SIS_EN (1 << 2)
  84. #define CMP_AUTOEN (1 << 1)
  85. #define BLEN (1 << 0)
  86. /* ADP8870_ALS1_EN Main ALS comparator level enable */
  87. #define L5_EN (1 << 3)
  88. #define L4_EN (1 << 2)
  89. #define L3_EN (1 << 1)
  90. #define L2_EN (1 << 0)
  91. #define CFGR_BLV_SHIFT 3
  92. #define CFGR_BLV_MASK 0x7
  93. #define ADP8870_FLAG_LED_MASK 0xFF
  94. #define FADE_VAL(in, out) ((0xF & (in)) | ((0xF & (out)) << 4))
  95. #define BL_CFGR_VAL(law, blv) ((((blv) & CFGR_BLV_MASK) << CFGR_BLV_SHIFT) | ((0x3 & (law)) << 1))
  96. #define ALS_CMPR_CFG_VAL(filt) ((0x7 & (filt)) << 1)
  97. struct adp8870_bl {
  98. struct i2c_client *client;
  99. struct backlight_device *bl;
  100. struct adp8870_led *led;
  101. struct adp8870_backlight_platform_data *pdata;
  102. struct mutex lock;
  103. unsigned long cached_daylight_max;
  104. int id;
  105. int revid;
  106. int current_brightness;
  107. };
  108. struct adp8870_led {
  109. struct led_classdev cdev;
  110. struct work_struct work;
  111. struct i2c_client *client;
  112. enum led_brightness new_brightness;
  113. int id;
  114. int flags;
  115. };
  116. static int adp8870_read(struct i2c_client *client, int reg, uint8_t *val)
  117. {
  118. int ret;
  119. ret = i2c_smbus_read_byte_data(client, reg);
  120. if (ret < 0) {
  121. dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
  122. return ret;
  123. }
  124. *val = ret;
  125. return 0;
  126. }
  127. static int adp8870_write(struct i2c_client *client, u8 reg, u8 val)
  128. {
  129. int ret = i2c_smbus_write_byte_data(client, reg, val);
  130. if (ret)
  131. dev_err(&client->dev, "failed to write\n");
  132. return ret;
  133. }
  134. static int adp8870_set_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
  135. {
  136. struct adp8870_bl *data = i2c_get_clientdata(client);
  137. uint8_t reg_val;
  138. int ret;
  139. mutex_lock(&data->lock);
  140. ret = adp8870_read(client, reg, &reg_val);
  141. if (!ret && ((reg_val & bit_mask) != bit_mask)) {
  142. reg_val |= bit_mask;
  143. ret = adp8870_write(client, reg, reg_val);
  144. }
  145. mutex_unlock(&data->lock);
  146. return ret;
  147. }
  148. static int adp8870_clr_bits(struct i2c_client *client, int reg, uint8_t bit_mask)
  149. {
  150. struct adp8870_bl *data = i2c_get_clientdata(client);
  151. uint8_t reg_val;
  152. int ret;
  153. mutex_lock(&data->lock);
  154. ret = adp8870_read(client, reg, &reg_val);
  155. if (!ret && (reg_val & bit_mask)) {
  156. reg_val &= ~bit_mask;
  157. ret = adp8870_write(client, reg, reg_val);
  158. }
  159. mutex_unlock(&data->lock);
  160. return ret;
  161. }
  162. /*
  163. * Independent sink / LED
  164. */
  165. #if defined(ADP8870_USE_LEDS)
  166. static void adp8870_led_work(struct work_struct *work)
  167. {
  168. struct adp8870_led *led = container_of(work, struct adp8870_led, work);
  169. adp8870_write(led->client, ADP8870_ISC1 + led->id - 1,
  170. led->new_brightness >> 1);
  171. }
  172. static void adp8870_led_set(struct led_classdev *led_cdev,
  173. enum led_brightness value)
  174. {
  175. struct adp8870_led *led;
  176. led = container_of(led_cdev, struct adp8870_led, cdev);
  177. led->new_brightness = value;
  178. /*
  179. * Use workqueue for IO since I2C operations can sleep.
  180. */
  181. schedule_work(&led->work);
  182. }
  183. static int adp8870_led_setup(struct adp8870_led *led)
  184. {
  185. struct i2c_client *client = led->client;
  186. int ret = 0;
  187. ret = adp8870_write(client, ADP8870_ISC1 + led->id - 1, 0);
  188. if (ret)
  189. return ret;
  190. ret = adp8870_set_bits(client, ADP8870_ISCC, 1 << (led->id - 1));
  191. if (ret)
  192. return ret;
  193. if (led->id > 4)
  194. ret = adp8870_set_bits(client, ADP8870_ISCT1,
  195. (led->flags & 0x3) << ((led->id - 5) * 2));
  196. else
  197. ret = adp8870_set_bits(client, ADP8870_ISCT2,
  198. (led->flags & 0x3) << ((led->id - 1) * 2));
  199. return ret;
  200. }
  201. static int adp8870_led_probe(struct i2c_client *client)
  202. {
  203. struct adp8870_backlight_platform_data *pdata =
  204. dev_get_platdata(&client->dev);
  205. struct adp8870_bl *data = i2c_get_clientdata(client);
  206. struct adp8870_led *led, *led_dat;
  207. struct led_info *cur_led;
  208. int ret, i;
  209. led = devm_kcalloc(&client->dev, pdata->num_leds, sizeof(*led),
  210. GFP_KERNEL);
  211. if (led == NULL)
  212. return -ENOMEM;
  213. ret = adp8870_write(client, ADP8870_ISCLAW, pdata->led_fade_law);
  214. if (ret)
  215. return ret;
  216. ret = adp8870_write(client, ADP8870_ISCT1,
  217. (pdata->led_on_time & 0x3) << 6);
  218. if (ret)
  219. return ret;
  220. ret = adp8870_write(client, ADP8870_ISCF,
  221. FADE_VAL(pdata->led_fade_in, pdata->led_fade_out));
  222. if (ret)
  223. return ret;
  224. for (i = 0; i < pdata->num_leds; ++i) {
  225. cur_led = &pdata->leds[i];
  226. led_dat = &led[i];
  227. led_dat->id = cur_led->flags & ADP8870_FLAG_LED_MASK;
  228. if (led_dat->id > 7 || led_dat->id < 1) {
  229. dev_err(&client->dev, "Invalid LED ID %d\n",
  230. led_dat->id);
  231. ret = -EINVAL;
  232. goto err;
  233. }
  234. if (pdata->bl_led_assign & (1 << (led_dat->id - 1))) {
  235. dev_err(&client->dev, "LED %d used by Backlight\n",
  236. led_dat->id);
  237. ret = -EBUSY;
  238. goto err;
  239. }
  240. led_dat->cdev.name = cur_led->name;
  241. led_dat->cdev.default_trigger = cur_led->default_trigger;
  242. led_dat->cdev.brightness_set = adp8870_led_set;
  243. led_dat->cdev.brightness = LED_OFF;
  244. led_dat->flags = cur_led->flags >> FLAG_OFFT_SHIFT;
  245. led_dat->client = client;
  246. led_dat->new_brightness = LED_OFF;
  247. INIT_WORK(&led_dat->work, adp8870_led_work);
  248. ret = led_classdev_register(&client->dev, &led_dat->cdev);
  249. if (ret) {
  250. dev_err(&client->dev, "failed to register LED %d\n",
  251. led_dat->id);
  252. goto err;
  253. }
  254. ret = adp8870_led_setup(led_dat);
  255. if (ret) {
  256. dev_err(&client->dev, "failed to write\n");
  257. i++;
  258. goto err;
  259. }
  260. }
  261. data->led = led;
  262. return 0;
  263. err:
  264. for (i = i - 1; i >= 0; --i) {
  265. led_classdev_unregister(&led[i].cdev);
  266. cancel_work_sync(&led[i].work);
  267. }
  268. return ret;
  269. }
  270. static int adp8870_led_remove(struct i2c_client *client)
  271. {
  272. struct adp8870_backlight_platform_data *pdata =
  273. dev_get_platdata(&client->dev);
  274. struct adp8870_bl *data = i2c_get_clientdata(client);
  275. int i;
  276. for (i = 0; i < pdata->num_leds; i++) {
  277. led_classdev_unregister(&data->led[i].cdev);
  278. cancel_work_sync(&data->led[i].work);
  279. }
  280. return 0;
  281. }
  282. #else
  283. static int adp8870_led_probe(struct i2c_client *client)
  284. {
  285. return 0;
  286. }
  287. static int adp8870_led_remove(struct i2c_client *client)
  288. {
  289. return 0;
  290. }
  291. #endif
  292. static int adp8870_bl_set(struct backlight_device *bl, int brightness)
  293. {
  294. struct adp8870_bl *data = bl_get_data(bl);
  295. struct i2c_client *client = data->client;
  296. int ret = 0;
  297. if (data->pdata->en_ambl_sens) {
  298. if ((brightness > 0) && (brightness < ADP8870_MAX_BRIGHTNESS)) {
  299. /* Disable Ambient Light auto adjust */
  300. ret = adp8870_clr_bits(client, ADP8870_MDCR,
  301. CMP_AUTOEN);
  302. if (ret)
  303. return ret;
  304. ret = adp8870_write(client, ADP8870_BLMX1, brightness);
  305. if (ret)
  306. return ret;
  307. } else {
  308. /*
  309. * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
  310. * restore daylight l1 sysfs brightness
  311. */
  312. ret = adp8870_write(client, ADP8870_BLMX1,
  313. data->cached_daylight_max);
  314. if (ret)
  315. return ret;
  316. ret = adp8870_set_bits(client, ADP8870_MDCR,
  317. CMP_AUTOEN);
  318. if (ret)
  319. return ret;
  320. }
  321. } else {
  322. ret = adp8870_write(client, ADP8870_BLMX1, brightness);
  323. if (ret)
  324. return ret;
  325. }
  326. if (data->current_brightness && brightness == 0)
  327. ret = adp8870_set_bits(client,
  328. ADP8870_MDCR, DIM_EN);
  329. else if (data->current_brightness == 0 && brightness)
  330. ret = adp8870_clr_bits(client,
  331. ADP8870_MDCR, DIM_EN);
  332. if (!ret)
  333. data->current_brightness = brightness;
  334. return ret;
  335. }
  336. static int adp8870_bl_update_status(struct backlight_device *bl)
  337. {
  338. return adp8870_bl_set(bl, backlight_get_brightness(bl));
  339. }
  340. static int adp8870_bl_get_brightness(struct backlight_device *bl)
  341. {
  342. struct adp8870_bl *data = bl_get_data(bl);
  343. return data->current_brightness;
  344. }
  345. static const struct backlight_ops adp8870_bl_ops = {
  346. .update_status = adp8870_bl_update_status,
  347. .get_brightness = adp8870_bl_get_brightness,
  348. };
  349. static int adp8870_bl_setup(struct backlight_device *bl)
  350. {
  351. struct adp8870_bl *data = bl_get_data(bl);
  352. struct i2c_client *client = data->client;
  353. struct adp8870_backlight_platform_data *pdata = data->pdata;
  354. int ret = 0;
  355. ret = adp8870_write(client, ADP8870_BLSEL, ~pdata->bl_led_assign);
  356. if (ret)
  357. return ret;
  358. ret = adp8870_write(client, ADP8870_PWMLED, pdata->pwm_assign);
  359. if (ret)
  360. return ret;
  361. ret = adp8870_write(client, ADP8870_BLMX1, pdata->l1_daylight_max);
  362. if (ret)
  363. return ret;
  364. ret = adp8870_write(client, ADP8870_BLDM1, pdata->l1_daylight_dim);
  365. if (ret)
  366. return ret;
  367. if (pdata->en_ambl_sens) {
  368. data->cached_daylight_max = pdata->l1_daylight_max;
  369. ret = adp8870_write(client, ADP8870_BLMX2,
  370. pdata->l2_bright_max);
  371. if (ret)
  372. return ret;
  373. ret = adp8870_write(client, ADP8870_BLDM2,
  374. pdata->l2_bright_dim);
  375. if (ret)
  376. return ret;
  377. ret = adp8870_write(client, ADP8870_BLMX3,
  378. pdata->l3_office_max);
  379. if (ret)
  380. return ret;
  381. ret = adp8870_write(client, ADP8870_BLDM3,
  382. pdata->l3_office_dim);
  383. if (ret)
  384. return ret;
  385. ret = adp8870_write(client, ADP8870_BLMX4,
  386. pdata->l4_indoor_max);
  387. if (ret)
  388. return ret;
  389. ret = adp8870_write(client, ADP8870_BLDM4,
  390. pdata->l4_indor_dim);
  391. if (ret)
  392. return ret;
  393. ret = adp8870_write(client, ADP8870_BLMX5,
  394. pdata->l5_dark_max);
  395. if (ret)
  396. return ret;
  397. ret = adp8870_write(client, ADP8870_BLDM5,
  398. pdata->l5_dark_dim);
  399. if (ret)
  400. return ret;
  401. ret = adp8870_write(client, ADP8870_L2TRP, pdata->l2_trip);
  402. if (ret)
  403. return ret;
  404. ret = adp8870_write(client, ADP8870_L2HYS, pdata->l2_hyst);
  405. if (ret)
  406. return ret;
  407. ret = adp8870_write(client, ADP8870_L3TRP, pdata->l3_trip);
  408. if (ret)
  409. return ret;
  410. ret = adp8870_write(client, ADP8870_L3HYS, pdata->l3_hyst);
  411. if (ret)
  412. return ret;
  413. ret = adp8870_write(client, ADP8870_L4TRP, pdata->l4_trip);
  414. if (ret)
  415. return ret;
  416. ret = adp8870_write(client, ADP8870_L4HYS, pdata->l4_hyst);
  417. if (ret)
  418. return ret;
  419. ret = adp8870_write(client, ADP8870_L5TRP, pdata->l5_trip);
  420. if (ret)
  421. return ret;
  422. ret = adp8870_write(client, ADP8870_L5HYS, pdata->l5_hyst);
  423. if (ret)
  424. return ret;
  425. ret = adp8870_write(client, ADP8870_ALS1_EN, L5_EN | L4_EN |
  426. L3_EN | L2_EN);
  427. if (ret)
  428. return ret;
  429. ret = adp8870_write(client, ADP8870_CMP_CTL,
  430. ALS_CMPR_CFG_VAL(pdata->abml_filt));
  431. if (ret)
  432. return ret;
  433. }
  434. ret = adp8870_write(client, ADP8870_CFGR,
  435. BL_CFGR_VAL(pdata->bl_fade_law, 0));
  436. if (ret)
  437. return ret;
  438. ret = adp8870_write(client, ADP8870_BLFR, FADE_VAL(pdata->bl_fade_in,
  439. pdata->bl_fade_out));
  440. if (ret)
  441. return ret;
  442. /*
  443. * ADP8870 Rev0 requires GDWN_DIS bit set
  444. */
  445. ret = adp8870_set_bits(client, ADP8870_MDCR, BLEN | DIM_EN | NSTBY |
  446. (data->revid == 0 ? GDWN_DIS : 0));
  447. return ret;
  448. }
  449. static ssize_t adp8870_show(struct device *dev, char *buf, int reg)
  450. {
  451. struct adp8870_bl *data = dev_get_drvdata(dev);
  452. int error;
  453. uint8_t reg_val;
  454. mutex_lock(&data->lock);
  455. error = adp8870_read(data->client, reg, &reg_val);
  456. mutex_unlock(&data->lock);
  457. if (error < 0)
  458. return error;
  459. return sprintf(buf, "%u\n", reg_val);
  460. }
  461. static ssize_t adp8870_store(struct device *dev, const char *buf,
  462. size_t count, int reg)
  463. {
  464. struct adp8870_bl *data = dev_get_drvdata(dev);
  465. unsigned long val;
  466. int ret;
  467. ret = kstrtoul(buf, 10, &val);
  468. if (ret)
  469. return ret;
  470. mutex_lock(&data->lock);
  471. adp8870_write(data->client, reg, val);
  472. mutex_unlock(&data->lock);
  473. return count;
  474. }
  475. static ssize_t adp8870_bl_l5_dark_max_show(struct device *dev,
  476. struct device_attribute *attr, char *buf)
  477. {
  478. return adp8870_show(dev, buf, ADP8870_BLMX5);
  479. }
  480. static ssize_t adp8870_bl_l5_dark_max_store(struct device *dev,
  481. struct device_attribute *attr, const char *buf, size_t count)
  482. {
  483. return adp8870_store(dev, buf, count, ADP8870_BLMX5);
  484. }
  485. static DEVICE_ATTR(l5_dark_max, 0664, adp8870_bl_l5_dark_max_show,
  486. adp8870_bl_l5_dark_max_store);
  487. static ssize_t adp8870_bl_l4_indoor_max_show(struct device *dev,
  488. struct device_attribute *attr, char *buf)
  489. {
  490. return adp8870_show(dev, buf, ADP8870_BLMX4);
  491. }
  492. static ssize_t adp8870_bl_l4_indoor_max_store(struct device *dev,
  493. struct device_attribute *attr, const char *buf, size_t count)
  494. {
  495. return adp8870_store(dev, buf, count, ADP8870_BLMX4);
  496. }
  497. static DEVICE_ATTR(l4_indoor_max, 0664, adp8870_bl_l4_indoor_max_show,
  498. adp8870_bl_l4_indoor_max_store);
  499. static ssize_t adp8870_bl_l3_office_max_show(struct device *dev,
  500. struct device_attribute *attr, char *buf)
  501. {
  502. return adp8870_show(dev, buf, ADP8870_BLMX3);
  503. }
  504. static ssize_t adp8870_bl_l3_office_max_store(struct device *dev,
  505. struct device_attribute *attr, const char *buf, size_t count)
  506. {
  507. return adp8870_store(dev, buf, count, ADP8870_BLMX3);
  508. }
  509. static DEVICE_ATTR(l3_office_max, 0664, adp8870_bl_l3_office_max_show,
  510. adp8870_bl_l3_office_max_store);
  511. static ssize_t adp8870_bl_l2_bright_max_show(struct device *dev,
  512. struct device_attribute *attr, char *buf)
  513. {
  514. return adp8870_show(dev, buf, ADP8870_BLMX2);
  515. }
  516. static ssize_t adp8870_bl_l2_bright_max_store(struct device *dev,
  517. struct device_attribute *attr, const char *buf, size_t count)
  518. {
  519. return adp8870_store(dev, buf, count, ADP8870_BLMX2);
  520. }
  521. static DEVICE_ATTR(l2_bright_max, 0664, adp8870_bl_l2_bright_max_show,
  522. adp8870_bl_l2_bright_max_store);
  523. static ssize_t adp8870_bl_l1_daylight_max_show(struct device *dev,
  524. struct device_attribute *attr, char *buf)
  525. {
  526. return adp8870_show(dev, buf, ADP8870_BLMX1);
  527. }
  528. static ssize_t adp8870_bl_l1_daylight_max_store(struct device *dev,
  529. struct device_attribute *attr, const char *buf, size_t count)
  530. {
  531. struct adp8870_bl *data = dev_get_drvdata(dev);
  532. int ret = kstrtoul(buf, 10, &data->cached_daylight_max);
  533. if (ret)
  534. return ret;
  535. return adp8870_store(dev, buf, count, ADP8870_BLMX1);
  536. }
  537. static DEVICE_ATTR(l1_daylight_max, 0664, adp8870_bl_l1_daylight_max_show,
  538. adp8870_bl_l1_daylight_max_store);
  539. static ssize_t adp8870_bl_l5_dark_dim_show(struct device *dev,
  540. struct device_attribute *attr, char *buf)
  541. {
  542. return adp8870_show(dev, buf, ADP8870_BLDM5);
  543. }
  544. static ssize_t adp8870_bl_l5_dark_dim_store(struct device *dev,
  545. struct device_attribute *attr,
  546. const char *buf, size_t count)
  547. {
  548. return adp8870_store(dev, buf, count, ADP8870_BLDM5);
  549. }
  550. static DEVICE_ATTR(l5_dark_dim, 0664, adp8870_bl_l5_dark_dim_show,
  551. adp8870_bl_l5_dark_dim_store);
  552. static ssize_t adp8870_bl_l4_indoor_dim_show(struct device *dev,
  553. struct device_attribute *attr, char *buf)
  554. {
  555. return adp8870_show(dev, buf, ADP8870_BLDM4);
  556. }
  557. static ssize_t adp8870_bl_l4_indoor_dim_store(struct device *dev,
  558. struct device_attribute *attr,
  559. const char *buf, size_t count)
  560. {
  561. return adp8870_store(dev, buf, count, ADP8870_BLDM4);
  562. }
  563. static DEVICE_ATTR(l4_indoor_dim, 0664, adp8870_bl_l4_indoor_dim_show,
  564. adp8870_bl_l4_indoor_dim_store);
  565. static ssize_t adp8870_bl_l3_office_dim_show(struct device *dev,
  566. struct device_attribute *attr, char *buf)
  567. {
  568. return adp8870_show(dev, buf, ADP8870_BLDM3);
  569. }
  570. static ssize_t adp8870_bl_l3_office_dim_store(struct device *dev,
  571. struct device_attribute *attr,
  572. const char *buf, size_t count)
  573. {
  574. return adp8870_store(dev, buf, count, ADP8870_BLDM3);
  575. }
  576. static DEVICE_ATTR(l3_office_dim, 0664, adp8870_bl_l3_office_dim_show,
  577. adp8870_bl_l3_office_dim_store);
  578. static ssize_t adp8870_bl_l2_bright_dim_show(struct device *dev,
  579. struct device_attribute *attr, char *buf)
  580. {
  581. return adp8870_show(dev, buf, ADP8870_BLDM2);
  582. }
  583. static ssize_t adp8870_bl_l2_bright_dim_store(struct device *dev,
  584. struct device_attribute *attr,
  585. const char *buf, size_t count)
  586. {
  587. return adp8870_store(dev, buf, count, ADP8870_BLDM2);
  588. }
  589. static DEVICE_ATTR(l2_bright_dim, 0664, adp8870_bl_l2_bright_dim_show,
  590. adp8870_bl_l2_bright_dim_store);
  591. static ssize_t adp8870_bl_l1_daylight_dim_show(struct device *dev,
  592. struct device_attribute *attr, char *buf)
  593. {
  594. return adp8870_show(dev, buf, ADP8870_BLDM1);
  595. }
  596. static ssize_t adp8870_bl_l1_daylight_dim_store(struct device *dev,
  597. struct device_attribute *attr,
  598. const char *buf, size_t count)
  599. {
  600. return adp8870_store(dev, buf, count, ADP8870_BLDM1);
  601. }
  602. static DEVICE_ATTR(l1_daylight_dim, 0664, adp8870_bl_l1_daylight_dim_show,
  603. adp8870_bl_l1_daylight_dim_store);
  604. #ifdef ADP8870_EXT_FEATURES
  605. static ssize_t adp8870_bl_ambient_light_level_show(struct device *dev,
  606. struct device_attribute *attr, char *buf)
  607. {
  608. struct adp8870_bl *data = dev_get_drvdata(dev);
  609. int error;
  610. uint8_t reg_val;
  611. uint16_t ret_val;
  612. mutex_lock(&data->lock);
  613. error = adp8870_read(data->client, ADP8870_PH1LEVL, &reg_val);
  614. if (error < 0) {
  615. mutex_unlock(&data->lock);
  616. return error;
  617. }
  618. ret_val = reg_val;
  619. error = adp8870_read(data->client, ADP8870_PH1LEVH, &reg_val);
  620. mutex_unlock(&data->lock);
  621. if (error < 0)
  622. return error;
  623. /* Return 13-bit conversion value for the first light sensor */
  624. ret_val += (reg_val & 0x1F) << 8;
  625. return sprintf(buf, "%u\n", ret_val);
  626. }
  627. static DEVICE_ATTR(ambient_light_level, 0444,
  628. adp8870_bl_ambient_light_level_show, NULL);
  629. static ssize_t adp8870_bl_ambient_light_zone_show(struct device *dev,
  630. struct device_attribute *attr, char *buf)
  631. {
  632. struct adp8870_bl *data = dev_get_drvdata(dev);
  633. int error;
  634. uint8_t reg_val;
  635. mutex_lock(&data->lock);
  636. error = adp8870_read(data->client, ADP8870_CFGR, &reg_val);
  637. mutex_unlock(&data->lock);
  638. if (error < 0)
  639. return error;
  640. return sprintf(buf, "%u\n",
  641. ((reg_val >> CFGR_BLV_SHIFT) & CFGR_BLV_MASK) + 1);
  642. }
  643. static ssize_t adp8870_bl_ambient_light_zone_store(struct device *dev,
  644. struct device_attribute *attr,
  645. const char *buf, size_t count)
  646. {
  647. struct adp8870_bl *data = dev_get_drvdata(dev);
  648. unsigned long val;
  649. uint8_t reg_val;
  650. int ret;
  651. ret = kstrtoul(buf, 10, &val);
  652. if (ret)
  653. return ret;
  654. if (val == 0) {
  655. /* Enable automatic ambient light sensing */
  656. adp8870_set_bits(data->client, ADP8870_MDCR, CMP_AUTOEN);
  657. } else if ((val > 0) && (val < 6)) {
  658. /* Disable automatic ambient light sensing */
  659. adp8870_clr_bits(data->client, ADP8870_MDCR, CMP_AUTOEN);
  660. /* Set user supplied ambient light zone */
  661. mutex_lock(&data->lock);
  662. ret = adp8870_read(data->client, ADP8870_CFGR, &reg_val);
  663. if (!ret) {
  664. reg_val &= ~(CFGR_BLV_MASK << CFGR_BLV_SHIFT);
  665. reg_val |= (val - 1) << CFGR_BLV_SHIFT;
  666. adp8870_write(data->client, ADP8870_CFGR, reg_val);
  667. }
  668. mutex_unlock(&data->lock);
  669. }
  670. return count;
  671. }
  672. static DEVICE_ATTR(ambient_light_zone, 0664,
  673. adp8870_bl_ambient_light_zone_show,
  674. adp8870_bl_ambient_light_zone_store);
  675. #endif
  676. static struct attribute *adp8870_bl_attributes[] = {
  677. &dev_attr_l5_dark_max.attr,
  678. &dev_attr_l5_dark_dim.attr,
  679. &dev_attr_l4_indoor_max.attr,
  680. &dev_attr_l4_indoor_dim.attr,
  681. &dev_attr_l3_office_max.attr,
  682. &dev_attr_l3_office_dim.attr,
  683. &dev_attr_l2_bright_max.attr,
  684. &dev_attr_l2_bright_dim.attr,
  685. &dev_attr_l1_daylight_max.attr,
  686. &dev_attr_l1_daylight_dim.attr,
  687. #ifdef ADP8870_EXT_FEATURES
  688. &dev_attr_ambient_light_level.attr,
  689. &dev_attr_ambient_light_zone.attr,
  690. #endif
  691. NULL
  692. };
  693. static const struct attribute_group adp8870_bl_attr_group = {
  694. .attrs = adp8870_bl_attributes,
  695. };
  696. static int adp8870_probe(struct i2c_client *client,
  697. const struct i2c_device_id *id)
  698. {
  699. struct backlight_properties props;
  700. struct backlight_device *bl;
  701. struct adp8870_bl *data;
  702. struct adp8870_backlight_platform_data *pdata =
  703. dev_get_platdata(&client->dev);
  704. uint8_t reg_val;
  705. int ret;
  706. if (!i2c_check_functionality(client->adapter,
  707. I2C_FUNC_SMBUS_BYTE_DATA)) {
  708. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  709. return -EIO;
  710. }
  711. if (!pdata) {
  712. dev_err(&client->dev, "no platform data?\n");
  713. return -EINVAL;
  714. }
  715. ret = adp8870_read(client, ADP8870_MFDVID, &reg_val);
  716. if (ret < 0)
  717. return -EIO;
  718. if (ADP8870_MANID(reg_val) != ADP8870_MANUFID) {
  719. dev_err(&client->dev, "failed to probe\n");
  720. return -ENODEV;
  721. }
  722. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  723. if (data == NULL)
  724. return -ENOMEM;
  725. data->revid = ADP8870_DEVID(reg_val);
  726. data->client = client;
  727. data->pdata = pdata;
  728. data->id = id->driver_data;
  729. data->current_brightness = 0;
  730. i2c_set_clientdata(client, data);
  731. mutex_init(&data->lock);
  732. memset(&props, 0, sizeof(props));
  733. props.type = BACKLIGHT_RAW;
  734. props.max_brightness = props.brightness = ADP8870_MAX_BRIGHTNESS;
  735. bl = devm_backlight_device_register(&client->dev,
  736. dev_driver_string(&client->dev),
  737. &client->dev, data, &adp8870_bl_ops, &props);
  738. if (IS_ERR(bl)) {
  739. dev_err(&client->dev, "failed to register backlight\n");
  740. return PTR_ERR(bl);
  741. }
  742. data->bl = bl;
  743. if (pdata->en_ambl_sens) {
  744. ret = sysfs_create_group(&bl->dev.kobj,
  745. &adp8870_bl_attr_group);
  746. if (ret) {
  747. dev_err(&client->dev, "failed to register sysfs\n");
  748. return ret;
  749. }
  750. }
  751. ret = adp8870_bl_setup(bl);
  752. if (ret) {
  753. ret = -EIO;
  754. goto out;
  755. }
  756. backlight_update_status(bl);
  757. dev_info(&client->dev, "Rev.%d Backlight\n", data->revid);
  758. if (pdata->num_leds)
  759. adp8870_led_probe(client);
  760. return 0;
  761. out:
  762. if (data->pdata->en_ambl_sens)
  763. sysfs_remove_group(&data->bl->dev.kobj,
  764. &adp8870_bl_attr_group);
  765. return ret;
  766. }
  767. static int adp8870_remove(struct i2c_client *client)
  768. {
  769. struct adp8870_bl *data = i2c_get_clientdata(client);
  770. adp8870_clr_bits(client, ADP8870_MDCR, NSTBY);
  771. if (data->led)
  772. adp8870_led_remove(client);
  773. if (data->pdata->en_ambl_sens)
  774. sysfs_remove_group(&data->bl->dev.kobj,
  775. &adp8870_bl_attr_group);
  776. return 0;
  777. }
  778. #ifdef CONFIG_PM_SLEEP
  779. static int adp8870_i2c_suspend(struct device *dev)
  780. {
  781. struct i2c_client *client = to_i2c_client(dev);
  782. adp8870_clr_bits(client, ADP8870_MDCR, NSTBY);
  783. return 0;
  784. }
  785. static int adp8870_i2c_resume(struct device *dev)
  786. {
  787. struct i2c_client *client = to_i2c_client(dev);
  788. adp8870_set_bits(client, ADP8870_MDCR, NSTBY | BLEN);
  789. return 0;
  790. }
  791. #endif
  792. static SIMPLE_DEV_PM_OPS(adp8870_i2c_pm_ops, adp8870_i2c_suspend,
  793. adp8870_i2c_resume);
  794. static const struct i2c_device_id adp8870_id[] = {
  795. { "adp8870", 0 },
  796. { }
  797. };
  798. MODULE_DEVICE_TABLE(i2c, adp8870_id);
  799. static struct i2c_driver adp8870_driver = {
  800. .driver = {
  801. .name = KBUILD_MODNAME,
  802. .pm = &adp8870_i2c_pm_ops,
  803. },
  804. .probe = adp8870_probe,
  805. .remove = adp8870_remove,
  806. .id_table = adp8870_id,
  807. };
  808. module_i2c_driver(adp8870_driver);
  809. MODULE_LICENSE("GPL v2");
  810. MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
  811. MODULE_DESCRIPTION("ADP8870 Backlight driver");