max8925_bl.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Backlight driver for Maxim MAX8925
  3. *
  4. * Copyright (C) 2009 Marvell International Ltd.
  5. * Haojian Zhuang <haojian.zhuang@marvell.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/fb.h>
  15. #include <linux/i2c.h>
  16. #include <linux/backlight.h>
  17. #include <linux/mfd/max8925.h>
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #define MAX_BRIGHTNESS (0xff)
  21. #define MIN_BRIGHTNESS (0)
  22. #define LWX_FREQ(x) (((x - 601) / 100) & 0x7)
  23. struct max8925_backlight_data {
  24. struct max8925_chip *chip;
  25. int current_brightness;
  26. int reg_mode_cntl;
  27. int reg_cntl;
  28. };
  29. static int max8925_backlight_set(struct backlight_device *bl, int brightness)
  30. {
  31. struct max8925_backlight_data *data = bl_get_data(bl);
  32. struct max8925_chip *chip = data->chip;
  33. unsigned char value;
  34. int ret;
  35. if (brightness > MAX_BRIGHTNESS)
  36. value = MAX_BRIGHTNESS;
  37. else
  38. value = brightness;
  39. ret = max8925_reg_write(chip->i2c, data->reg_cntl, value);
  40. if (ret < 0)
  41. goto out;
  42. if (!data->current_brightness && brightness)
  43. /* enable WLED output */
  44. ret = max8925_set_bits(chip->i2c, data->reg_mode_cntl, 1, 1);
  45. else if (!brightness)
  46. /* disable WLED output */
  47. ret = max8925_set_bits(chip->i2c, data->reg_mode_cntl, 1, 0);
  48. if (ret < 0)
  49. goto out;
  50. dev_dbg(chip->dev, "set brightness %d\n", value);
  51. data->current_brightness = value;
  52. return 0;
  53. out:
  54. dev_dbg(chip->dev, "set brightness %d failure with return value:%d\n",
  55. value, ret);
  56. return ret;
  57. }
  58. static int max8925_backlight_update_status(struct backlight_device *bl)
  59. {
  60. int brightness = bl->props.brightness;
  61. if (bl->props.power != FB_BLANK_UNBLANK)
  62. brightness = 0;
  63. if (bl->props.fb_blank != FB_BLANK_UNBLANK)
  64. brightness = 0;
  65. if (bl->props.state & BL_CORE_SUSPENDED)
  66. brightness = 0;
  67. return max8925_backlight_set(bl, brightness);
  68. }
  69. static int max8925_backlight_get_brightness(struct backlight_device *bl)
  70. {
  71. struct max8925_backlight_data *data = bl_get_data(bl);
  72. struct max8925_chip *chip = data->chip;
  73. int ret;
  74. ret = max8925_reg_read(chip->i2c, data->reg_cntl);
  75. if (ret < 0)
  76. return -EINVAL;
  77. data->current_brightness = ret;
  78. dev_dbg(chip->dev, "get brightness %d\n", data->current_brightness);
  79. return ret;
  80. }
  81. static const struct backlight_ops max8925_backlight_ops = {
  82. .options = BL_CORE_SUSPENDRESUME,
  83. .update_status = max8925_backlight_update_status,
  84. .get_brightness = max8925_backlight_get_brightness,
  85. };
  86. static void max8925_backlight_dt_init(struct platform_device *pdev)
  87. {
  88. struct device_node *nproot = pdev->dev.parent->of_node, *np;
  89. struct max8925_backlight_pdata *pdata;
  90. u32 val;
  91. if (!nproot || !IS_ENABLED(CONFIG_OF))
  92. return;
  93. pdata = devm_kzalloc(&pdev->dev,
  94. sizeof(struct max8925_backlight_pdata),
  95. GFP_KERNEL);
  96. if (!pdata)
  97. return;
  98. np = of_get_child_by_name(nproot, "backlight");
  99. if (!np) {
  100. dev_err(&pdev->dev, "failed to find backlight node\n");
  101. return;
  102. }
  103. if (!of_property_read_u32(np, "maxim,max8925-dual-string", &val))
  104. pdata->dual_string = val;
  105. of_node_put(np);
  106. pdev->dev.platform_data = pdata;
  107. }
  108. static int max8925_backlight_probe(struct platform_device *pdev)
  109. {
  110. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  111. struct max8925_backlight_pdata *pdata;
  112. struct max8925_backlight_data *data;
  113. struct backlight_device *bl;
  114. struct backlight_properties props;
  115. struct resource *res;
  116. unsigned char value;
  117. int ret = 0;
  118. data = devm_kzalloc(&pdev->dev, sizeof(struct max8925_backlight_data),
  119. GFP_KERNEL);
  120. if (data == NULL)
  121. return -ENOMEM;
  122. res = platform_get_resource(pdev, IORESOURCE_REG, 0);
  123. if (!res) {
  124. dev_err(&pdev->dev, "No REG resource for mode control!\n");
  125. return -ENXIO;
  126. }
  127. data->reg_mode_cntl = res->start;
  128. res = platform_get_resource(pdev, IORESOURCE_REG, 1);
  129. if (!res) {
  130. dev_err(&pdev->dev, "No REG resource for control!\n");
  131. return -ENXIO;
  132. }
  133. data->reg_cntl = res->start;
  134. data->chip = chip;
  135. data->current_brightness = 0;
  136. memset(&props, 0, sizeof(struct backlight_properties));
  137. props.type = BACKLIGHT_RAW;
  138. props.max_brightness = MAX_BRIGHTNESS;
  139. bl = devm_backlight_device_register(&pdev->dev, "max8925-backlight",
  140. &pdev->dev, data,
  141. &max8925_backlight_ops, &props);
  142. if (IS_ERR(bl)) {
  143. dev_err(&pdev->dev, "failed to register backlight\n");
  144. return PTR_ERR(bl);
  145. }
  146. bl->props.brightness = MAX_BRIGHTNESS;
  147. platform_set_drvdata(pdev, bl);
  148. value = 0;
  149. if (!pdev->dev.platform_data)
  150. max8925_backlight_dt_init(pdev);
  151. pdata = pdev->dev.platform_data;
  152. if (pdata) {
  153. if (pdata->lxw_scl)
  154. value |= (1 << 7);
  155. if (pdata->lxw_freq)
  156. value |= (LWX_FREQ(pdata->lxw_freq) << 4);
  157. if (pdata->dual_string)
  158. value |= (1 << 1);
  159. }
  160. ret = max8925_set_bits(chip->i2c, data->reg_mode_cntl, 0xfe, value);
  161. if (ret < 0)
  162. return ret;
  163. backlight_update_status(bl);
  164. return 0;
  165. }
  166. static struct platform_driver max8925_backlight_driver = {
  167. .driver = {
  168. .name = "max8925-backlight",
  169. },
  170. .probe = max8925_backlight_probe,
  171. };
  172. module_platform_driver(max8925_backlight_driver);
  173. MODULE_DESCRIPTION("Backlight Driver for Maxim MAX8925");
  174. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  175. MODULE_LICENSE("GPL");
  176. MODULE_ALIAS("platform:max8925-backlight");