adp8870_bl.c 26 KB

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