adp8870_bl.c 26 KB

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