as3711_bl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * AS3711 PMIC backlight driver, using DCDC Step Up Converters
  3. *
  4. * Copyright (C) 2012 Renesas Electronics Corporation
  5. * Author: Guennadi Liakhovetski, <g.liakhovetski@gmx.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation
  10. */
  11. #include <linux/backlight.h>
  12. #include <linux/delay.h>
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/fb.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mfd/as3711.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. enum as3711_bl_type {
  23. AS3711_BL_SU1,
  24. AS3711_BL_SU2,
  25. };
  26. struct as3711_bl_data {
  27. bool powered;
  28. const char *fb_name;
  29. struct device *fb_dev;
  30. enum as3711_bl_type type;
  31. int brightness;
  32. struct backlight_device *bl;
  33. };
  34. struct as3711_bl_supply {
  35. struct as3711_bl_data su1;
  36. struct as3711_bl_data su2;
  37. const struct as3711_bl_pdata *pdata;
  38. struct as3711 *as3711;
  39. };
  40. static struct as3711_bl_supply *to_supply(struct as3711_bl_data *su)
  41. {
  42. switch (su->type) {
  43. case AS3711_BL_SU1:
  44. return container_of(su, struct as3711_bl_supply, su1);
  45. case AS3711_BL_SU2:
  46. return container_of(su, struct as3711_bl_supply, su2);
  47. }
  48. return NULL;
  49. }
  50. static int as3711_set_brightness_auto_i(struct as3711_bl_data *data,
  51. unsigned int brightness)
  52. {
  53. struct as3711_bl_supply *supply = to_supply(data);
  54. struct as3711 *as3711 = supply->as3711;
  55. const struct as3711_bl_pdata *pdata = supply->pdata;
  56. int ret = 0;
  57. /* Only all equal current values are supported */
  58. if (pdata->su2_auto_curr1)
  59. ret = regmap_write(as3711->regmap, AS3711_CURR1_VALUE,
  60. brightness);
  61. if (!ret && pdata->su2_auto_curr2)
  62. ret = regmap_write(as3711->regmap, AS3711_CURR2_VALUE,
  63. brightness);
  64. if (!ret && pdata->su2_auto_curr3)
  65. ret = regmap_write(as3711->regmap, AS3711_CURR3_VALUE,
  66. brightness);
  67. return ret;
  68. }
  69. static int as3711_set_brightness_v(struct as3711 *as3711,
  70. unsigned int brightness,
  71. unsigned int reg)
  72. {
  73. if (brightness > 31)
  74. return -EINVAL;
  75. return regmap_update_bits(as3711->regmap, reg, 0xf0,
  76. brightness << 4);
  77. }
  78. static int as3711_bl_su2_reset(struct as3711_bl_supply *supply)
  79. {
  80. struct as3711 *as3711 = supply->as3711;
  81. int ret = regmap_update_bits(as3711->regmap, AS3711_STEPUP_CONTROL_5,
  82. 3, supply->pdata->su2_fbprot);
  83. if (!ret)
  84. ret = regmap_update_bits(as3711->regmap,
  85. AS3711_STEPUP_CONTROL_2, 1, 0);
  86. if (!ret)
  87. ret = regmap_update_bits(as3711->regmap,
  88. AS3711_STEPUP_CONTROL_2, 1, 1);
  89. return ret;
  90. }
  91. /*
  92. * Someone with less fragile or less expensive hardware could try to simplify
  93. * the brightness adjustment procedure.
  94. */
  95. static int as3711_bl_update_status(struct backlight_device *bl)
  96. {
  97. struct as3711_bl_data *data = bl_get_data(bl);
  98. struct as3711_bl_supply *supply = to_supply(data);
  99. struct as3711 *as3711 = supply->as3711;
  100. int brightness = bl->props.brightness;
  101. int ret = 0;
  102. dev_dbg(&bl->dev, "%s(): brightness %u, pwr %x, blank %x, state %x\n",
  103. __func__, bl->props.brightness, bl->props.power,
  104. bl->props.fb_blank, bl->props.state);
  105. if (bl->props.power != FB_BLANK_UNBLANK ||
  106. bl->props.fb_blank != FB_BLANK_UNBLANK ||
  107. bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
  108. brightness = 0;
  109. if (data->type == AS3711_BL_SU1) {
  110. ret = as3711_set_brightness_v(as3711, brightness,
  111. AS3711_STEPUP_CONTROL_1);
  112. } else {
  113. const struct as3711_bl_pdata *pdata = supply->pdata;
  114. switch (pdata->su2_feedback) {
  115. case AS3711_SU2_VOLTAGE:
  116. ret = as3711_set_brightness_v(as3711, brightness,
  117. AS3711_STEPUP_CONTROL_2);
  118. break;
  119. case AS3711_SU2_CURR_AUTO:
  120. ret = as3711_set_brightness_auto_i(data, brightness / 4);
  121. if (ret < 0)
  122. return ret;
  123. if (brightness) {
  124. ret = as3711_bl_su2_reset(supply);
  125. if (ret < 0)
  126. return ret;
  127. udelay(500);
  128. ret = as3711_set_brightness_auto_i(data, brightness);
  129. } else {
  130. ret = regmap_update_bits(as3711->regmap,
  131. AS3711_STEPUP_CONTROL_2, 1, 0);
  132. }
  133. break;
  134. /* Manual one current feedback pin below */
  135. case AS3711_SU2_CURR1:
  136. ret = regmap_write(as3711->regmap, AS3711_CURR1_VALUE,
  137. brightness);
  138. break;
  139. case AS3711_SU2_CURR2:
  140. ret = regmap_write(as3711->regmap, AS3711_CURR2_VALUE,
  141. brightness);
  142. break;
  143. case AS3711_SU2_CURR3:
  144. ret = regmap_write(as3711->regmap, AS3711_CURR3_VALUE,
  145. brightness);
  146. break;
  147. default:
  148. ret = -EINVAL;
  149. }
  150. }
  151. if (!ret)
  152. data->brightness = brightness;
  153. return ret;
  154. }
  155. static int as3711_bl_get_brightness(struct backlight_device *bl)
  156. {
  157. struct as3711_bl_data *data = bl_get_data(bl);
  158. return data->brightness;
  159. }
  160. static const struct backlight_ops as3711_bl_ops = {
  161. .update_status = as3711_bl_update_status,
  162. .get_brightness = as3711_bl_get_brightness,
  163. };
  164. static int as3711_bl_init_su2(struct as3711_bl_supply *supply)
  165. {
  166. struct as3711 *as3711 = supply->as3711;
  167. const struct as3711_bl_pdata *pdata = supply->pdata;
  168. u8 ctl = 0;
  169. int ret;
  170. dev_dbg(as3711->dev, "%s(): use %u\n", __func__, pdata->su2_feedback);
  171. /* Turn SU2 off */
  172. ret = regmap_write(as3711->regmap, AS3711_STEPUP_CONTROL_2, 0);
  173. if (ret < 0)
  174. return ret;
  175. switch (pdata->su2_feedback) {
  176. case AS3711_SU2_VOLTAGE:
  177. ret = regmap_update_bits(as3711->regmap, AS3711_STEPUP_CONTROL_4, 3, 0);
  178. break;
  179. case AS3711_SU2_CURR1:
  180. ctl = 1;
  181. ret = regmap_update_bits(as3711->regmap, AS3711_STEPUP_CONTROL_4, 3, 1);
  182. break;
  183. case AS3711_SU2_CURR2:
  184. ctl = 4;
  185. ret = regmap_update_bits(as3711->regmap, AS3711_STEPUP_CONTROL_4, 3, 2);
  186. break;
  187. case AS3711_SU2_CURR3:
  188. ctl = 0x10;
  189. ret = regmap_update_bits(as3711->regmap, AS3711_STEPUP_CONTROL_4, 3, 3);
  190. break;
  191. case AS3711_SU2_CURR_AUTO:
  192. if (pdata->su2_auto_curr1)
  193. ctl = 2;
  194. if (pdata->su2_auto_curr2)
  195. ctl |= 8;
  196. if (pdata->su2_auto_curr3)
  197. ctl |= 0x20;
  198. ret = 0;
  199. break;
  200. default:
  201. return -EINVAL;
  202. }
  203. if (!ret)
  204. ret = regmap_write(as3711->regmap, AS3711_CURR_CONTROL, ctl);
  205. return ret;
  206. }
  207. static int as3711_bl_register(struct platform_device *pdev,
  208. unsigned int max_brightness, struct as3711_bl_data *su)
  209. {
  210. struct backlight_properties props = {.type = BACKLIGHT_RAW,};
  211. struct backlight_device *bl;
  212. /* max tuning I = 31uA for voltage- and 38250uA for current-feedback */
  213. props.max_brightness = max_brightness;
  214. bl = devm_backlight_device_register(&pdev->dev,
  215. su->type == AS3711_BL_SU1 ?
  216. "as3711-su1" : "as3711-su2",
  217. &pdev->dev, su,
  218. &as3711_bl_ops, &props);
  219. if (IS_ERR(bl)) {
  220. dev_err(&pdev->dev, "failed to register backlight\n");
  221. return PTR_ERR(bl);
  222. }
  223. bl->props.brightness = props.max_brightness;
  224. backlight_update_status(bl);
  225. su->bl = bl;
  226. return 0;
  227. }
  228. static int as3711_backlight_parse_dt(struct device *dev)
  229. {
  230. struct as3711_bl_pdata *pdata = dev_get_platdata(dev);
  231. struct device_node *bl, *fb;
  232. int ret;
  233. bl = of_get_child_by_name(dev->parent->of_node, "backlight");
  234. if (!bl) {
  235. dev_dbg(dev, "backlight node not found\n");
  236. return -ENODEV;
  237. }
  238. fb = of_parse_phandle(bl, "su1-dev", 0);
  239. if (fb) {
  240. pdata->su1_fb = fb->full_name;
  241. ret = of_property_read_u32(bl, "su1-max-uA", &pdata->su1_max_uA);
  242. if (pdata->su1_max_uA <= 0)
  243. ret = -EINVAL;
  244. if (ret < 0)
  245. goto err_put_bl;
  246. }
  247. fb = of_parse_phandle(bl, "su2-dev", 0);
  248. if (fb) {
  249. int count = 0;
  250. pdata->su2_fb = fb->full_name;
  251. ret = of_property_read_u32(bl, "su2-max-uA", &pdata->su2_max_uA);
  252. if (pdata->su2_max_uA <= 0)
  253. ret = -EINVAL;
  254. if (ret < 0)
  255. goto err_put_bl;
  256. if (of_find_property(bl, "su2-feedback-voltage", NULL)) {
  257. pdata->su2_feedback = AS3711_SU2_VOLTAGE;
  258. count++;
  259. }
  260. if (of_find_property(bl, "su2-feedback-curr1", NULL)) {
  261. pdata->su2_feedback = AS3711_SU2_CURR1;
  262. count++;
  263. }
  264. if (of_find_property(bl, "su2-feedback-curr2", NULL)) {
  265. pdata->su2_feedback = AS3711_SU2_CURR2;
  266. count++;
  267. }
  268. if (of_find_property(bl, "su2-feedback-curr3", NULL)) {
  269. pdata->su2_feedback = AS3711_SU2_CURR3;
  270. count++;
  271. }
  272. if (of_find_property(bl, "su2-feedback-curr-auto", NULL)) {
  273. pdata->su2_feedback = AS3711_SU2_CURR_AUTO;
  274. count++;
  275. }
  276. if (count != 1) {
  277. ret = -EINVAL;
  278. goto err_put_bl;
  279. }
  280. count = 0;
  281. if (of_find_property(bl, "su2-fbprot-lx-sd4", NULL)) {
  282. pdata->su2_fbprot = AS3711_SU2_LX_SD4;
  283. count++;
  284. }
  285. if (of_find_property(bl, "su2-fbprot-gpio2", NULL)) {
  286. pdata->su2_fbprot = AS3711_SU2_GPIO2;
  287. count++;
  288. }
  289. if (of_find_property(bl, "su2-fbprot-gpio3", NULL)) {
  290. pdata->su2_fbprot = AS3711_SU2_GPIO3;
  291. count++;
  292. }
  293. if (of_find_property(bl, "su2-fbprot-gpio4", NULL)) {
  294. pdata->su2_fbprot = AS3711_SU2_GPIO4;
  295. count++;
  296. }
  297. if (count != 1) {
  298. ret = -EINVAL;
  299. goto err_put_bl;
  300. }
  301. count = 0;
  302. if (of_find_property(bl, "su2-auto-curr1", NULL)) {
  303. pdata->su2_auto_curr1 = true;
  304. count++;
  305. }
  306. if (of_find_property(bl, "su2-auto-curr2", NULL)) {
  307. pdata->su2_auto_curr2 = true;
  308. count++;
  309. }
  310. if (of_find_property(bl, "su2-auto-curr3", NULL)) {
  311. pdata->su2_auto_curr3 = true;
  312. count++;
  313. }
  314. /*
  315. * At least one su2-auto-curr* must be specified iff
  316. * AS3711_SU2_CURR_AUTO is used
  317. */
  318. if (!count ^ (pdata->su2_feedback != AS3711_SU2_CURR_AUTO)) {
  319. ret = -EINVAL;
  320. goto err_put_bl;
  321. }
  322. }
  323. of_node_put(bl);
  324. return 0;
  325. err_put_bl:
  326. of_node_put(bl);
  327. return ret;
  328. }
  329. static int as3711_backlight_probe(struct platform_device *pdev)
  330. {
  331. struct as3711_bl_pdata *pdata = dev_get_platdata(&pdev->dev);
  332. struct as3711 *as3711 = dev_get_drvdata(pdev->dev.parent);
  333. struct as3711_bl_supply *supply;
  334. struct as3711_bl_data *su;
  335. unsigned int max_brightness;
  336. int ret;
  337. if (!pdata) {
  338. dev_err(&pdev->dev, "No platform data, exiting...\n");
  339. return -ENODEV;
  340. }
  341. if (pdev->dev.parent->of_node) {
  342. ret = as3711_backlight_parse_dt(&pdev->dev);
  343. if (ret < 0) {
  344. dev_err(&pdev->dev, "DT parsing failed: %d\n", ret);
  345. return ret;
  346. }
  347. }
  348. if (!pdata->su1_fb && !pdata->su2_fb) {
  349. dev_err(&pdev->dev, "No framebuffer specified\n");
  350. return -EINVAL;
  351. }
  352. /*
  353. * Due to possible hardware damage I chose to block all modes,
  354. * unsupported on my hardware. Anyone, wishing to use any of those modes
  355. * will have to first review the code, then activate and test it.
  356. */
  357. if (pdata->su1_fb ||
  358. pdata->su2_fbprot != AS3711_SU2_GPIO4 ||
  359. pdata->su2_feedback != AS3711_SU2_CURR_AUTO) {
  360. dev_warn(&pdev->dev,
  361. "Attention! An untested mode has been chosen!\n"
  362. "Please, review the code, enable, test, and report success:-)\n");
  363. return -EINVAL;
  364. }
  365. supply = devm_kzalloc(&pdev->dev, sizeof(*supply), GFP_KERNEL);
  366. if (!supply)
  367. return -ENOMEM;
  368. supply->as3711 = as3711;
  369. supply->pdata = pdata;
  370. if (pdata->su1_fb) {
  371. su = &supply->su1;
  372. su->fb_name = pdata->su1_fb;
  373. su->type = AS3711_BL_SU1;
  374. max_brightness = min(pdata->su1_max_uA, 31);
  375. ret = as3711_bl_register(pdev, max_brightness, su);
  376. if (ret < 0)
  377. return ret;
  378. }
  379. if (pdata->su2_fb) {
  380. su = &supply->su2;
  381. su->fb_name = pdata->su2_fb;
  382. su->type = AS3711_BL_SU2;
  383. switch (pdata->su2_fbprot) {
  384. case AS3711_SU2_GPIO2:
  385. case AS3711_SU2_GPIO3:
  386. case AS3711_SU2_GPIO4:
  387. case AS3711_SU2_LX_SD4:
  388. break;
  389. default:
  390. return -EINVAL;
  391. }
  392. switch (pdata->su2_feedback) {
  393. case AS3711_SU2_VOLTAGE:
  394. max_brightness = min(pdata->su2_max_uA, 31);
  395. break;
  396. case AS3711_SU2_CURR1:
  397. case AS3711_SU2_CURR2:
  398. case AS3711_SU2_CURR3:
  399. case AS3711_SU2_CURR_AUTO:
  400. max_brightness = min(pdata->su2_max_uA / 150, 255);
  401. break;
  402. default:
  403. return -EINVAL;
  404. }
  405. ret = as3711_bl_init_su2(supply);
  406. if (ret < 0)
  407. return ret;
  408. ret = as3711_bl_register(pdev, max_brightness, su);
  409. if (ret < 0)
  410. return ret;
  411. }
  412. platform_set_drvdata(pdev, supply);
  413. return 0;
  414. }
  415. static struct platform_driver as3711_backlight_driver = {
  416. .driver = {
  417. .name = "as3711-backlight",
  418. },
  419. .probe = as3711_backlight_probe,
  420. };
  421. module_platform_driver(as3711_backlight_driver);
  422. MODULE_DESCRIPTION("Backlight Driver for AS3711 PMICs");
  423. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de");
  424. MODULE_LICENSE("GPL");
  425. MODULE_ALIAS("platform:as3711-backlight");