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. client->dev.platform_data;
  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. dev_err(&client->dev, "failed to alloc memory\n");
  214. return -ENOMEM;
  215. }
  216. ret = adp8870_write(client, ADP8870_ISCLAW, pdata->led_fade_law);
  217. if (ret)
  218. return ret;
  219. ret = adp8870_write(client, ADP8870_ISCT1,
  220. (pdata->led_on_time & 0x3) << 6);
  221. if (ret)
  222. return ret;
  223. ret = adp8870_write(client, ADP8870_ISCF,
  224. FADE_VAL(pdata->led_fade_in, pdata->led_fade_out));
  225. if (ret)
  226. return ret;
  227. for (i = 0; i < pdata->num_leds; ++i) {
  228. cur_led = &pdata->leds[i];
  229. led_dat = &led[i];
  230. led_dat->id = cur_led->flags & ADP8870_FLAG_LED_MASK;
  231. if (led_dat->id > 7 || led_dat->id < 1) {
  232. dev_err(&client->dev, "Invalid LED ID %d\n",
  233. led_dat->id);
  234. ret = -EINVAL;
  235. goto err;
  236. }
  237. if (pdata->bl_led_assign & (1 << (led_dat->id - 1))) {
  238. dev_err(&client->dev, "LED %d used by Backlight\n",
  239. led_dat->id);
  240. ret = -EBUSY;
  241. goto err;
  242. }
  243. led_dat->cdev.name = cur_led->name;
  244. led_dat->cdev.default_trigger = cur_led->default_trigger;
  245. led_dat->cdev.brightness_set = adp8870_led_set;
  246. led_dat->cdev.brightness = LED_OFF;
  247. led_dat->flags = cur_led->flags >> FLAG_OFFT_SHIFT;
  248. led_dat->client = client;
  249. led_dat->new_brightness = LED_OFF;
  250. INIT_WORK(&led_dat->work, adp8870_led_work);
  251. ret = led_classdev_register(&client->dev, &led_dat->cdev);
  252. if (ret) {
  253. dev_err(&client->dev, "failed to register LED %d\n",
  254. led_dat->id);
  255. goto err;
  256. }
  257. ret = adp8870_led_setup(led_dat);
  258. if (ret) {
  259. dev_err(&client->dev, "failed to write\n");
  260. i++;
  261. goto err;
  262. }
  263. }
  264. data->led = led;
  265. return 0;
  266. err:
  267. for (i = i - 1; i >= 0; --i) {
  268. led_classdev_unregister(&led[i].cdev);
  269. cancel_work_sync(&led[i].work);
  270. }
  271. return ret;
  272. }
  273. static int adp8870_led_remove(struct i2c_client *client)
  274. {
  275. struct adp8870_backlight_platform_data *pdata =
  276. client->dev.platform_data;
  277. struct adp8870_bl *data = i2c_get_clientdata(client);
  278. int i;
  279. for (i = 0; i < pdata->num_leds; i++) {
  280. led_classdev_unregister(&data->led[i].cdev);
  281. cancel_work_sync(&data->led[i].work);
  282. }
  283. return 0;
  284. }
  285. #else
  286. static int adp8870_led_probe(struct i2c_client *client)
  287. {
  288. return 0;
  289. }
  290. static int adp8870_led_remove(struct i2c_client *client)
  291. {
  292. return 0;
  293. }
  294. #endif
  295. static int adp8870_bl_set(struct backlight_device *bl, int brightness)
  296. {
  297. struct adp8870_bl *data = bl_get_data(bl);
  298. struct i2c_client *client = data->client;
  299. int ret = 0;
  300. if (data->pdata->en_ambl_sens) {
  301. if ((brightness > 0) && (brightness < ADP8870_MAX_BRIGHTNESS)) {
  302. /* Disable Ambient Light auto adjust */
  303. ret = adp8870_clr_bits(client, ADP8870_MDCR,
  304. CMP_AUTOEN);
  305. if (ret)
  306. return ret;
  307. ret = adp8870_write(client, ADP8870_BLMX1, brightness);
  308. if (ret)
  309. return ret;
  310. } else {
  311. /*
  312. * MAX_BRIGHTNESS -> Enable Ambient Light auto adjust
  313. * restore daylight l1 sysfs brightness
  314. */
  315. ret = adp8870_write(client, ADP8870_BLMX1,
  316. data->cached_daylight_max);
  317. if (ret)
  318. return ret;
  319. ret = adp8870_set_bits(client, ADP8870_MDCR,
  320. CMP_AUTOEN);
  321. if (ret)
  322. return ret;
  323. }
  324. } else {
  325. ret = adp8870_write(client, ADP8870_BLMX1, brightness);
  326. if (ret)
  327. return ret;
  328. }
  329. if (data->current_brightness && brightness == 0)
  330. ret = adp8870_set_bits(client,
  331. ADP8870_MDCR, DIM_EN);
  332. else if (data->current_brightness == 0 && brightness)
  333. ret = adp8870_clr_bits(client,
  334. ADP8870_MDCR, DIM_EN);
  335. if (!ret)
  336. data->current_brightness = brightness;
  337. return ret;
  338. }
  339. static int adp8870_bl_update_status(struct backlight_device *bl)
  340. {
  341. int brightness = bl->props.brightness;
  342. if (bl->props.power != FB_BLANK_UNBLANK)
  343. brightness = 0;
  344. if (bl->props.fb_blank != FB_BLANK_UNBLANK)
  345. brightness = 0;
  346. return adp8870_bl_set(bl, brightness);
  347. }
  348. static int adp8870_bl_get_brightness(struct backlight_device *bl)
  349. {
  350. struct adp8870_bl *data = bl_get_data(bl);
  351. return data->current_brightness;
  352. }
  353. static const struct backlight_ops adp8870_bl_ops = {
  354. .update_status = adp8870_bl_update_status,
  355. .get_brightness = adp8870_bl_get_brightness,
  356. };
  357. static int adp8870_bl_setup(struct backlight_device *bl)
  358. {
  359. struct adp8870_bl *data = bl_get_data(bl);
  360. struct i2c_client *client = data->client;
  361. struct adp8870_backlight_platform_data *pdata = data->pdata;
  362. int ret = 0;
  363. ret = adp8870_write(client, ADP8870_BLSEL, ~pdata->bl_led_assign);
  364. if (ret)
  365. return ret;
  366. ret = adp8870_write(client, ADP8870_PWMLED, pdata->pwm_assign);
  367. if (ret)
  368. return ret;
  369. ret = adp8870_write(client, ADP8870_BLMX1, pdata->l1_daylight_max);
  370. if (ret)
  371. return ret;
  372. ret = adp8870_write(client, ADP8870_BLDM1, pdata->l1_daylight_dim);
  373. if (ret)
  374. return ret;
  375. if (pdata->en_ambl_sens) {
  376. data->cached_daylight_max = pdata->l1_daylight_max;
  377. ret = adp8870_write(client, ADP8870_BLMX2,
  378. pdata->l2_bright_max);
  379. if (ret)
  380. return ret;
  381. ret = adp8870_write(client, ADP8870_BLDM2,
  382. pdata->l2_bright_dim);
  383. if (ret)
  384. return ret;
  385. ret = adp8870_write(client, ADP8870_BLMX3,
  386. pdata->l3_office_max);
  387. if (ret)
  388. return ret;
  389. ret = adp8870_write(client, ADP8870_BLDM3,
  390. pdata->l3_office_dim);
  391. if (ret)
  392. return ret;
  393. ret = adp8870_write(client, ADP8870_BLMX4,
  394. pdata->l4_indoor_max);
  395. if (ret)
  396. return ret;
  397. ret = adp8870_write(client, ADP8870_BLDM4,
  398. pdata->l4_indor_dim);
  399. if (ret)
  400. return ret;
  401. ret = adp8870_write(client, ADP8870_BLMX5,
  402. pdata->l5_dark_max);
  403. if (ret)
  404. return ret;
  405. ret = adp8870_write(client, ADP8870_BLDM5,
  406. pdata->l5_dark_dim);
  407. if (ret)
  408. return ret;
  409. ret = adp8870_write(client, ADP8870_L2TRP, pdata->l2_trip);
  410. if (ret)
  411. return ret;
  412. ret = adp8870_write(client, ADP8870_L2HYS, pdata->l2_hyst);
  413. if (ret)
  414. return ret;
  415. ret = adp8870_write(client, ADP8870_L3TRP, pdata->l3_trip);
  416. if (ret)
  417. return ret;
  418. ret = adp8870_write(client, ADP8870_L3HYS, pdata->l3_hyst);
  419. if (ret)
  420. return ret;
  421. ret = adp8870_write(client, ADP8870_L4TRP, pdata->l4_trip);
  422. if (ret)
  423. return ret;
  424. ret = adp8870_write(client, ADP8870_L4HYS, pdata->l4_hyst);
  425. if (ret)
  426. return ret;
  427. ret = adp8870_write(client, ADP8870_L5TRP, pdata->l5_trip);
  428. if (ret)
  429. return ret;
  430. ret = adp8870_write(client, ADP8870_L5HYS, pdata->l5_hyst);
  431. if (ret)
  432. return ret;
  433. ret = adp8870_write(client, ADP8870_ALS1_EN, L5_EN | L4_EN |
  434. L3_EN | L2_EN);
  435. if (ret)
  436. return ret;
  437. ret = adp8870_write(client, ADP8870_CMP_CTL,
  438. ALS_CMPR_CFG_VAL(pdata->abml_filt));
  439. if (ret)
  440. return ret;
  441. }
  442. ret = adp8870_write(client, ADP8870_CFGR,
  443. BL_CFGR_VAL(pdata->bl_fade_law, 0));
  444. if (ret)
  445. return ret;
  446. ret = adp8870_write(client, ADP8870_BLFR, FADE_VAL(pdata->bl_fade_in,
  447. pdata->bl_fade_out));
  448. if (ret)
  449. return ret;
  450. /*
  451. * ADP8870 Rev0 requires GDWN_DIS bit set
  452. */
  453. ret = adp8870_set_bits(client, ADP8870_MDCR, BLEN | DIM_EN | NSTBY |
  454. (data->revid == 0 ? GDWN_DIS : 0));
  455. return ret;
  456. }
  457. static ssize_t adp8870_show(struct device *dev, char *buf, int reg)
  458. {
  459. struct adp8870_bl *data = dev_get_drvdata(dev);
  460. int error;
  461. uint8_t reg_val;
  462. mutex_lock(&data->lock);
  463. error = adp8870_read(data->client, reg, &reg_val);
  464. mutex_unlock(&data->lock);
  465. if (error < 0)
  466. return error;
  467. return sprintf(buf, "%u\n", reg_val);
  468. }
  469. static ssize_t adp8870_store(struct device *dev, const char *buf,
  470. size_t count, int reg)
  471. {
  472. struct adp8870_bl *data = dev_get_drvdata(dev);
  473. unsigned long val;
  474. int ret;
  475. ret = kstrtoul(buf, 10, &val);
  476. if (ret)
  477. return ret;
  478. mutex_lock(&data->lock);
  479. adp8870_write(data->client, reg, val);
  480. mutex_unlock(&data->lock);
  481. return count;
  482. }
  483. static ssize_t adp8870_bl_l5_dark_max_show(struct device *dev,
  484. struct device_attribute *attr, char *buf)
  485. {
  486. return adp8870_show(dev, buf, ADP8870_BLMX5);
  487. }
  488. static ssize_t adp8870_bl_l5_dark_max_store(struct device *dev,
  489. struct device_attribute *attr, const char *buf, size_t count)
  490. {
  491. return adp8870_store(dev, buf, count, ADP8870_BLMX5);
  492. }
  493. static DEVICE_ATTR(l5_dark_max, 0664, adp8870_bl_l5_dark_max_show,
  494. adp8870_bl_l5_dark_max_store);
  495. static ssize_t adp8870_bl_l4_indoor_max_show(struct device *dev,
  496. struct device_attribute *attr, char *buf)
  497. {
  498. return adp8870_show(dev, buf, ADP8870_BLMX4);
  499. }
  500. static ssize_t adp8870_bl_l4_indoor_max_store(struct device *dev,
  501. struct device_attribute *attr, const char *buf, size_t count)
  502. {
  503. return adp8870_store(dev, buf, count, ADP8870_BLMX4);
  504. }
  505. static DEVICE_ATTR(l4_indoor_max, 0664, adp8870_bl_l4_indoor_max_show,
  506. adp8870_bl_l4_indoor_max_store);
  507. static ssize_t adp8870_bl_l3_office_max_show(struct device *dev,
  508. struct device_attribute *attr, char *buf)
  509. {
  510. return adp8870_show(dev, buf, ADP8870_BLMX3);
  511. }
  512. static ssize_t adp8870_bl_l3_office_max_store(struct device *dev,
  513. struct device_attribute *attr, const char *buf, size_t count)
  514. {
  515. return adp8870_store(dev, buf, count, ADP8870_BLMX3);
  516. }
  517. static DEVICE_ATTR(l3_office_max, 0664, adp8870_bl_l3_office_max_show,
  518. adp8870_bl_l3_office_max_store);
  519. static ssize_t adp8870_bl_l2_bright_max_show(struct device *dev,
  520. struct device_attribute *attr, char *buf)
  521. {
  522. return adp8870_show(dev, buf, ADP8870_BLMX2);
  523. }
  524. static ssize_t adp8870_bl_l2_bright_max_store(struct device *dev,
  525. struct device_attribute *attr, const char *buf, size_t count)
  526. {
  527. return adp8870_store(dev, buf, count, ADP8870_BLMX2);
  528. }
  529. static DEVICE_ATTR(l2_bright_max, 0664, adp8870_bl_l2_bright_max_show,
  530. adp8870_bl_l2_bright_max_store);
  531. static ssize_t adp8870_bl_l1_daylight_max_show(struct device *dev,
  532. struct device_attribute *attr, char *buf)
  533. {
  534. return adp8870_show(dev, buf, ADP8870_BLMX1);
  535. }
  536. static ssize_t adp8870_bl_l1_daylight_max_store(struct device *dev,
  537. struct device_attribute *attr, const char *buf, size_t count)
  538. {
  539. struct adp8870_bl *data = dev_get_drvdata(dev);
  540. int ret = kstrtoul(buf, 10, &data->cached_daylight_max);
  541. if (ret)
  542. return ret;
  543. return adp8870_store(dev, buf, count, ADP8870_BLMX1);
  544. }
  545. static DEVICE_ATTR(l1_daylight_max, 0664, adp8870_bl_l1_daylight_max_show,
  546. adp8870_bl_l1_daylight_max_store);
  547. static ssize_t adp8870_bl_l5_dark_dim_show(struct device *dev,
  548. struct device_attribute *attr, char *buf)
  549. {
  550. return adp8870_show(dev, buf, ADP8870_BLDM5);
  551. }
  552. static ssize_t adp8870_bl_l5_dark_dim_store(struct device *dev,
  553. struct device_attribute *attr,
  554. const char *buf, size_t count)
  555. {
  556. return adp8870_store(dev, buf, count, ADP8870_BLDM5);
  557. }
  558. static DEVICE_ATTR(l5_dark_dim, 0664, adp8870_bl_l5_dark_dim_show,
  559. adp8870_bl_l5_dark_dim_store);
  560. static ssize_t adp8870_bl_l4_indoor_dim_show(struct device *dev,
  561. struct device_attribute *attr, char *buf)
  562. {
  563. return adp8870_show(dev, buf, ADP8870_BLDM4);
  564. }
  565. static ssize_t adp8870_bl_l4_indoor_dim_store(struct device *dev,
  566. struct device_attribute *attr,
  567. const char *buf, size_t count)
  568. {
  569. return adp8870_store(dev, buf, count, ADP8870_BLDM4);
  570. }
  571. static DEVICE_ATTR(l4_indoor_dim, 0664, adp8870_bl_l4_indoor_dim_show,
  572. adp8870_bl_l4_indoor_dim_store);
  573. static ssize_t adp8870_bl_l3_office_dim_show(struct device *dev,
  574. struct device_attribute *attr, char *buf)
  575. {
  576. return adp8870_show(dev, buf, ADP8870_BLDM3);
  577. }
  578. static ssize_t adp8870_bl_l3_office_dim_store(struct device *dev,
  579. struct device_attribute *attr,
  580. const char *buf, size_t count)
  581. {
  582. return adp8870_store(dev, buf, count, ADP8870_BLDM3);
  583. }
  584. static DEVICE_ATTR(l3_office_dim, 0664, adp8870_bl_l3_office_dim_show,
  585. adp8870_bl_l3_office_dim_store);
  586. static ssize_t adp8870_bl_l2_bright_dim_show(struct device *dev,
  587. struct device_attribute *attr, char *buf)
  588. {
  589. return adp8870_show(dev, buf, ADP8870_BLDM2);
  590. }
  591. static ssize_t adp8870_bl_l2_bright_dim_store(struct device *dev,
  592. struct device_attribute *attr,
  593. const char *buf, size_t count)
  594. {
  595. return adp8870_store(dev, buf, count, ADP8870_BLDM2);
  596. }
  597. static DEVICE_ATTR(l2_bright_dim, 0664, adp8870_bl_l2_bright_dim_show,
  598. adp8870_bl_l2_bright_dim_store);
  599. static ssize_t adp8870_bl_l1_daylight_dim_show(struct device *dev,
  600. struct device_attribute *attr, char *buf)
  601. {
  602. return adp8870_show(dev, buf, ADP8870_BLDM1);
  603. }
  604. static ssize_t adp8870_bl_l1_daylight_dim_store(struct device *dev,
  605. struct device_attribute *attr,
  606. const char *buf, size_t count)
  607. {
  608. return adp8870_store(dev, buf, count, ADP8870_BLDM1);
  609. }
  610. static DEVICE_ATTR(l1_daylight_dim, 0664, adp8870_bl_l1_daylight_dim_show,
  611. adp8870_bl_l1_daylight_dim_store);
  612. #ifdef ADP8870_EXT_FEATURES
  613. static ssize_t adp8870_bl_ambient_light_level_show(struct device *dev,
  614. struct device_attribute *attr, char *buf)
  615. {
  616. struct adp8870_bl *data = dev_get_drvdata(dev);
  617. int error;
  618. uint8_t reg_val;
  619. uint16_t ret_val;
  620. mutex_lock(&data->lock);
  621. error = adp8870_read(data->client, ADP8870_PH1LEVL, &reg_val);
  622. if (error < 0) {
  623. mutex_unlock(&data->lock);
  624. return error;
  625. }
  626. ret_val = reg_val;
  627. error = adp8870_read(data->client, ADP8870_PH1LEVH, &reg_val);
  628. mutex_unlock(&data->lock);
  629. if (error < 0)
  630. return error;
  631. /* Return 13-bit conversion value for the first light sensor */
  632. ret_val += (reg_val & 0x1F) << 8;
  633. return sprintf(buf, "%u\n", ret_val);
  634. }
  635. static DEVICE_ATTR(ambient_light_level, 0444,
  636. adp8870_bl_ambient_light_level_show, NULL);
  637. static ssize_t adp8870_bl_ambient_light_zone_show(struct device *dev,
  638. struct device_attribute *attr, char *buf)
  639. {
  640. struct adp8870_bl *data = dev_get_drvdata(dev);
  641. int error;
  642. uint8_t reg_val;
  643. mutex_lock(&data->lock);
  644. error = adp8870_read(data->client, ADP8870_CFGR, &reg_val);
  645. mutex_unlock(&data->lock);
  646. if (error < 0)
  647. return error;
  648. return sprintf(buf, "%u\n",
  649. ((reg_val >> CFGR_BLV_SHIFT) & CFGR_BLV_MASK) + 1);
  650. }
  651. static ssize_t adp8870_bl_ambient_light_zone_store(struct device *dev,
  652. struct device_attribute *attr,
  653. const char *buf, size_t count)
  654. {
  655. struct adp8870_bl *data = dev_get_drvdata(dev);
  656. unsigned long val;
  657. uint8_t reg_val;
  658. int ret;
  659. ret = kstrtoul(buf, 10, &val);
  660. if (ret)
  661. return ret;
  662. if (val == 0) {
  663. /* Enable automatic ambient light sensing */
  664. adp8870_set_bits(data->client, ADP8870_MDCR, CMP_AUTOEN);
  665. } else if ((val > 0) && (val < 6)) {
  666. /* Disable automatic ambient light sensing */
  667. adp8870_clr_bits(data->client, ADP8870_MDCR, CMP_AUTOEN);
  668. /* Set user supplied ambient light zone */
  669. mutex_lock(&data->lock);
  670. adp8870_read(data->client, ADP8870_CFGR, &reg_val);
  671. reg_val &= ~(CFGR_BLV_MASK << CFGR_BLV_SHIFT);
  672. reg_val |= (val - 1) << CFGR_BLV_SHIFT;
  673. adp8870_write(data->client, ADP8870_CFGR, reg_val);
  674. mutex_unlock(&data->lock);
  675. }
  676. return count;
  677. }
  678. static DEVICE_ATTR(ambient_light_zone, 0664,
  679. adp8870_bl_ambient_light_zone_show,
  680. adp8870_bl_ambient_light_zone_store);
  681. #endif
  682. static struct attribute *adp8870_bl_attributes[] = {
  683. &dev_attr_l5_dark_max.attr,
  684. &dev_attr_l5_dark_dim.attr,
  685. &dev_attr_l4_indoor_max.attr,
  686. &dev_attr_l4_indoor_dim.attr,
  687. &dev_attr_l3_office_max.attr,
  688. &dev_attr_l3_office_dim.attr,
  689. &dev_attr_l2_bright_max.attr,
  690. &dev_attr_l2_bright_dim.attr,
  691. &dev_attr_l1_daylight_max.attr,
  692. &dev_attr_l1_daylight_dim.attr,
  693. #ifdef ADP8870_EXT_FEATURES
  694. &dev_attr_ambient_light_level.attr,
  695. &dev_attr_ambient_light_zone.attr,
  696. #endif
  697. NULL
  698. };
  699. static const struct attribute_group adp8870_bl_attr_group = {
  700. .attrs = adp8870_bl_attributes,
  701. };
  702. static int adp8870_probe(struct i2c_client *client,
  703. const struct i2c_device_id *id)
  704. {
  705. struct backlight_properties props;
  706. struct backlight_device *bl;
  707. struct adp8870_bl *data;
  708. struct adp8870_backlight_platform_data *pdata =
  709. client->dev.platform_data;
  710. uint8_t reg_val;
  711. int ret;
  712. if (!i2c_check_functionality(client->adapter,
  713. I2C_FUNC_SMBUS_BYTE_DATA)) {
  714. dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
  715. return -EIO;
  716. }
  717. if (!pdata) {
  718. dev_err(&client->dev, "no platform data?\n");
  719. return -EINVAL;
  720. }
  721. ret = adp8870_read(client, ADP8870_MFDVID, &reg_val);
  722. if (ret < 0)
  723. return -EIO;
  724. if (ADP8870_MANID(reg_val) != ADP8870_MANUFID) {
  725. dev_err(&client->dev, "failed to probe\n");
  726. return -ENODEV;
  727. }
  728. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  729. if (data == NULL)
  730. return -ENOMEM;
  731. data->revid = ADP8870_DEVID(reg_val);
  732. data->client = client;
  733. data->pdata = pdata;
  734. data->id = id->driver_data;
  735. data->current_brightness = 0;
  736. i2c_set_clientdata(client, data);
  737. mutex_init(&data->lock);
  738. memset(&props, 0, sizeof(props));
  739. props.type = BACKLIGHT_RAW;
  740. props.max_brightness = props.brightness = ADP8870_MAX_BRIGHTNESS;
  741. bl = backlight_device_register(dev_driver_string(&client->dev),
  742. &client->dev, data, &adp8870_bl_ops, &props);
  743. if (IS_ERR(bl)) {
  744. dev_err(&client->dev, "failed to register backlight\n");
  745. return PTR_ERR(bl);
  746. }
  747. data->bl = bl;
  748. if (pdata->en_ambl_sens) {
  749. ret = sysfs_create_group(&bl->dev.kobj,
  750. &adp8870_bl_attr_group);
  751. if (ret) {
  752. dev_err(&client->dev, "failed to register sysfs\n");
  753. goto out1;
  754. }
  755. }
  756. ret = adp8870_bl_setup(bl);
  757. if (ret) {
  758. ret = -EIO;
  759. goto out;
  760. }
  761. backlight_update_status(bl);
  762. dev_info(&client->dev, "Rev.%d Backlight\n", data->revid);
  763. if (pdata->num_leds)
  764. adp8870_led_probe(client);
  765. return 0;
  766. out:
  767. if (data->pdata->en_ambl_sens)
  768. sysfs_remove_group(&data->bl->dev.kobj,
  769. &adp8870_bl_attr_group);
  770. out1:
  771. backlight_device_unregister(bl);
  772. return ret;
  773. }
  774. static int adp8870_remove(struct i2c_client *client)
  775. {
  776. struct adp8870_bl *data = i2c_get_clientdata(client);
  777. adp8870_clr_bits(client, ADP8870_MDCR, NSTBY);
  778. if (data->led)
  779. adp8870_led_remove(client);
  780. if (data->pdata->en_ambl_sens)
  781. sysfs_remove_group(&data->bl->dev.kobj,
  782. &adp8870_bl_attr_group);
  783. backlight_device_unregister(data->bl);
  784. return 0;
  785. }
  786. #ifdef CONFIG_PM_SLEEP
  787. static int adp8870_i2c_suspend(struct device *dev)
  788. {
  789. struct i2c_client *client = to_i2c_client(dev);
  790. adp8870_clr_bits(client, ADP8870_MDCR, NSTBY);
  791. return 0;
  792. }
  793. static int adp8870_i2c_resume(struct device *dev)
  794. {
  795. struct i2c_client *client = to_i2c_client(dev);
  796. adp8870_set_bits(client, ADP8870_MDCR, NSTBY | BLEN);
  797. return 0;
  798. }
  799. #endif
  800. static SIMPLE_DEV_PM_OPS(adp8870_i2c_pm_ops, adp8870_i2c_suspend,
  801. adp8870_i2c_resume);
  802. static const struct i2c_device_id adp8870_id[] = {
  803. { "adp8870", 0 },
  804. { }
  805. };
  806. MODULE_DEVICE_TABLE(i2c, adp8870_id);
  807. static struct i2c_driver adp8870_driver = {
  808. .driver = {
  809. .name = KBUILD_MODNAME,
  810. .pm = &adp8870_i2c_pm_ops,
  811. },
  812. .probe = adp8870_probe,
  813. .remove = adp8870_remove,
  814. .id_table = adp8870_id,
  815. };
  816. module_i2c_driver(adp8870_driver);
  817. MODULE_LICENSE("GPL v2");
  818. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  819. MODULE_DESCRIPTION("ADP8870 Backlight driver");
  820. MODULE_ALIAS("i2c:adp8870-backlight");