sky81452-backlight.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * sky81452-backlight.c SKY81452 backlight driver
  3. *
  4. * Copyright 2014 Skyworks Solutions Inc.
  5. * Author : Gyungoh Yoo <jack.yoo@skyworksinc.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/backlight.h>
  20. #include <linux/err.h>
  21. #include <linux/gpio.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/of.h>
  26. #include <linux/of_gpio.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/regmap.h>
  29. #include <linux/platform_data/sky81452-backlight.h>
  30. #include <linux/slab.h>
  31. /* registers */
  32. #define SKY81452_REG0 0x00
  33. #define SKY81452_REG1 0x01
  34. #define SKY81452_REG2 0x02
  35. #define SKY81452_REG4 0x04
  36. #define SKY81452_REG5 0x05
  37. /* bit mask */
  38. #define SKY81452_CS 0xFF
  39. #define SKY81452_EN 0x3F
  40. #define SKY81452_IGPW 0x20
  41. #define SKY81452_PWMMD 0x10
  42. #define SKY81452_PHASE 0x08
  43. #define SKY81452_ILIM 0x04
  44. #define SKY81452_VSHRT 0x03
  45. #define SKY81452_OCP 0x80
  46. #define SKY81452_OTMP 0x40
  47. #define SKY81452_SHRT 0x3F
  48. #define SKY81452_OPN 0x3F
  49. #define SKY81452_DEFAULT_NAME "lcd-backlight"
  50. #define SKY81452_MAX_BRIGHTNESS (SKY81452_CS + 1)
  51. #define CTZ(b) __builtin_ctz(b)
  52. static int sky81452_bl_update_status(struct backlight_device *bd)
  53. {
  54. const struct sky81452_bl_platform_data *pdata =
  55. dev_get_platdata(bd->dev.parent);
  56. const unsigned int brightness = (unsigned int)bd->props.brightness;
  57. struct regmap *regmap = bl_get_data(bd);
  58. int ret;
  59. if (brightness > 0) {
  60. ret = regmap_write(regmap, SKY81452_REG0, brightness - 1);
  61. if (IS_ERR_VALUE(ret))
  62. return ret;
  63. return regmap_update_bits(regmap, SKY81452_REG1, SKY81452_EN,
  64. pdata->enable << CTZ(SKY81452_EN));
  65. }
  66. return regmap_update_bits(regmap, SKY81452_REG1, SKY81452_EN, 0);
  67. }
  68. static const struct backlight_ops sky81452_bl_ops = {
  69. .update_status = sky81452_bl_update_status,
  70. };
  71. static ssize_t sky81452_bl_store_enable(struct device *dev,
  72. struct device_attribute *attr, const char *buf, size_t count)
  73. {
  74. struct regmap *regmap = bl_get_data(to_backlight_device(dev));
  75. unsigned long value;
  76. int ret;
  77. ret = kstrtoul(buf, 16, &value);
  78. if (IS_ERR_VALUE(ret))
  79. return ret;
  80. ret = regmap_update_bits(regmap, SKY81452_REG1, SKY81452_EN,
  81. value << CTZ(SKY81452_EN));
  82. if (IS_ERR_VALUE(ret))
  83. return ret;
  84. return count;
  85. }
  86. static ssize_t sky81452_bl_show_open_short(struct device *dev,
  87. struct device_attribute *attr, char *buf)
  88. {
  89. struct regmap *regmap = bl_get_data(to_backlight_device(dev));
  90. unsigned int reg, value = 0;
  91. char tmp[3];
  92. int i, ret;
  93. reg = !strcmp(attr->attr.name, "open") ? SKY81452_REG5 : SKY81452_REG4;
  94. ret = regmap_read(regmap, reg, &value);
  95. if (IS_ERR_VALUE(ret))
  96. return ret;
  97. if (value & SKY81452_SHRT) {
  98. *buf = 0;
  99. for (i = 0; i < 6; i++) {
  100. if (value & 0x01) {
  101. sprintf(tmp, "%d ", i + 1);
  102. strcat(buf, tmp);
  103. }
  104. value >>= 1;
  105. }
  106. strcat(buf, "\n");
  107. } else {
  108. strcpy(buf, "none\n");
  109. }
  110. return strlen(buf);
  111. }
  112. static ssize_t sky81452_bl_show_fault(struct device *dev,
  113. struct device_attribute *attr, char *buf)
  114. {
  115. struct regmap *regmap = bl_get_data(to_backlight_device(dev));
  116. unsigned int value = 0;
  117. int ret;
  118. ret = regmap_read(regmap, SKY81452_REG4, &value);
  119. if (IS_ERR_VALUE(ret))
  120. return ret;
  121. *buf = 0;
  122. if (value & SKY81452_OCP)
  123. strcat(buf, "over-current ");
  124. if (value & SKY81452_OTMP)
  125. strcat(buf, "over-temperature");
  126. strcat(buf, "\n");
  127. return strlen(buf);
  128. }
  129. static DEVICE_ATTR(enable, S_IWGRP | S_IWUSR, NULL, sky81452_bl_store_enable);
  130. static DEVICE_ATTR(open, S_IRUGO, sky81452_bl_show_open_short, NULL);
  131. static DEVICE_ATTR(short, S_IRUGO, sky81452_bl_show_open_short, NULL);
  132. static DEVICE_ATTR(fault, S_IRUGO, sky81452_bl_show_fault, NULL);
  133. static struct attribute *sky81452_bl_attribute[] = {
  134. &dev_attr_enable.attr,
  135. &dev_attr_open.attr,
  136. &dev_attr_short.attr,
  137. &dev_attr_fault.attr,
  138. NULL
  139. };
  140. static const struct attribute_group sky81452_bl_attr_group = {
  141. .attrs = sky81452_bl_attribute,
  142. };
  143. #ifdef CONFIG_OF
  144. static struct sky81452_bl_platform_data *sky81452_bl_parse_dt(
  145. struct device *dev)
  146. {
  147. struct device_node *np = of_node_get(dev->of_node);
  148. struct sky81452_bl_platform_data *pdata;
  149. int num_entry;
  150. unsigned int sources[6];
  151. int ret;
  152. if (!np) {
  153. dev_err(dev, "backlight node not found.\n");
  154. return ERR_PTR(-ENODATA);
  155. }
  156. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  157. if (!pdata) {
  158. of_node_put(np);
  159. return ERR_PTR(-ENOMEM);
  160. }
  161. of_property_read_string(np, "name", &pdata->name);
  162. pdata->ignore_pwm = of_property_read_bool(np, "skyworks,ignore-pwm");
  163. pdata->dpwm_mode = of_property_read_bool(np, "skyworks,dpwm-mode");
  164. pdata->phase_shift = of_property_read_bool(np, "skyworks,phase-shift");
  165. pdata->gpio_enable = of_get_gpio(np, 0);
  166. ret = of_property_count_u32_elems(np, "led-sources");
  167. if (IS_ERR_VALUE(ret)) {
  168. pdata->enable = SKY81452_EN >> CTZ(SKY81452_EN);
  169. } else {
  170. num_entry = ret;
  171. if (num_entry > 6)
  172. num_entry = 6;
  173. ret = of_property_read_u32_array(np, "led-sources", sources,
  174. num_entry);
  175. if (IS_ERR_VALUE(ret)) {
  176. dev_err(dev, "led-sources node is invalid.\n");
  177. return ERR_PTR(-EINVAL);
  178. }
  179. pdata->enable = 0;
  180. while (--num_entry)
  181. pdata->enable |= (1 << sources[num_entry]);
  182. }
  183. ret = of_property_read_u32(np,
  184. "skyworks,short-detection-threshold-volt",
  185. &pdata->short_detection_threshold);
  186. if (IS_ERR_VALUE(ret))
  187. pdata->short_detection_threshold = 7;
  188. ret = of_property_read_u32(np, "skyworks,current-limit-mA",
  189. &pdata->boost_current_limit);
  190. if (IS_ERR_VALUE(ret))
  191. pdata->boost_current_limit = 2750;
  192. of_node_put(np);
  193. return pdata;
  194. }
  195. #else
  196. static struct sky81452_bl_platform_data *sky81452_bl_parse_dt(
  197. struct device *dev)
  198. {
  199. return ERR_PTR(-EINVAL);
  200. }
  201. #endif
  202. static int sky81452_bl_init_device(struct regmap *regmap,
  203. struct sky81452_bl_platform_data *pdata)
  204. {
  205. unsigned int value;
  206. value = pdata->ignore_pwm ? SKY81452_IGPW : 0;
  207. value |= pdata->dpwm_mode ? SKY81452_PWMMD : 0;
  208. value |= pdata->phase_shift ? 0 : SKY81452_PHASE;
  209. if (pdata->boost_current_limit == 2300)
  210. value |= SKY81452_ILIM;
  211. else if (pdata->boost_current_limit != 2750)
  212. return -EINVAL;
  213. if (pdata->short_detection_threshold < 4 ||
  214. pdata->short_detection_threshold > 7)
  215. return -EINVAL;
  216. value |= (7 - pdata->short_detection_threshold) << CTZ(SKY81452_VSHRT);
  217. return regmap_write(regmap, SKY81452_REG2, value);
  218. }
  219. static int sky81452_bl_probe(struct platform_device *pdev)
  220. {
  221. struct device *dev = &pdev->dev;
  222. struct regmap *regmap = dev_get_drvdata(dev->parent);
  223. struct sky81452_bl_platform_data *pdata = dev_get_platdata(dev);
  224. struct backlight_device *bd;
  225. struct backlight_properties props;
  226. const char *name;
  227. int ret;
  228. if (!pdata) {
  229. pdata = sky81452_bl_parse_dt(dev);
  230. if (IS_ERR(pdata))
  231. return PTR_ERR(pdata);
  232. }
  233. if (gpio_is_valid(pdata->gpio_enable)) {
  234. ret = devm_gpio_request_one(dev, pdata->gpio_enable,
  235. GPIOF_OUT_INIT_HIGH, "sky81452-en");
  236. if (IS_ERR_VALUE(ret)) {
  237. dev_err(dev, "failed to request GPIO. err=%d\n", ret);
  238. return ret;
  239. }
  240. }
  241. ret = sky81452_bl_init_device(regmap, pdata);
  242. if (IS_ERR_VALUE(ret)) {
  243. dev_err(dev, "failed to initialize. err=%d\n", ret);
  244. return ret;
  245. }
  246. memset(&props, 0, sizeof(props));
  247. props.max_brightness = SKY81452_MAX_BRIGHTNESS,
  248. name = pdata->name ? pdata->name : SKY81452_DEFAULT_NAME;
  249. bd = devm_backlight_device_register(dev, name, dev, regmap,
  250. &sky81452_bl_ops, &props);
  251. if (IS_ERR(bd)) {
  252. dev_err(dev, "failed to register. err=%ld\n", PTR_ERR(bd));
  253. return PTR_ERR(bd);
  254. }
  255. platform_set_drvdata(pdev, bd);
  256. ret = sysfs_create_group(&bd->dev.kobj, &sky81452_bl_attr_group);
  257. if (IS_ERR_VALUE(ret)) {
  258. dev_err(dev, "failed to create attribute. err=%d\n", ret);
  259. return ret;
  260. }
  261. return ret;
  262. }
  263. static int sky81452_bl_remove(struct platform_device *pdev)
  264. {
  265. const struct sky81452_bl_platform_data *pdata =
  266. dev_get_platdata(&pdev->dev);
  267. struct backlight_device *bd = platform_get_drvdata(pdev);
  268. sysfs_remove_group(&bd->dev.kobj, &sky81452_bl_attr_group);
  269. bd->props.power = FB_BLANK_UNBLANK;
  270. bd->props.brightness = 0;
  271. backlight_update_status(bd);
  272. if (gpio_is_valid(pdata->gpio_enable))
  273. gpio_set_value_cansleep(pdata->gpio_enable, 0);
  274. return 0;
  275. }
  276. #ifdef CONFIG_OF
  277. static const struct of_device_id sky81452_bl_of_match[] = {
  278. { .compatible = "skyworks,sky81452-backlight", },
  279. { }
  280. };
  281. MODULE_DEVICE_TABLE(of, sky81452_bl_of_match);
  282. #endif
  283. static struct platform_driver sky81452_bl_driver = {
  284. .driver = {
  285. .name = "sky81452-backlight",
  286. .of_match_table = of_match_ptr(sky81452_bl_of_match),
  287. },
  288. .probe = sky81452_bl_probe,
  289. .remove = sky81452_bl_remove,
  290. };
  291. module_platform_driver(sky81452_bl_driver);
  292. MODULE_DESCRIPTION("Skyworks SKY81452 backlight driver");
  293. MODULE_AUTHOR("Gyungoh Yoo <jack.yoo@skyworksinc.com>");
  294. MODULE_LICENSE("GPL v2");