max8925_bl.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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_find_node_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. pdev->dev.platform_data = pdata;
  106. }
  107. static int max8925_backlight_probe(struct platform_device *pdev)
  108. {
  109. struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
  110. struct max8925_backlight_pdata *pdata;
  111. struct max8925_backlight_data *data;
  112. struct backlight_device *bl;
  113. struct backlight_properties props;
  114. struct resource *res;
  115. unsigned char value;
  116. int ret = 0;
  117. data = devm_kzalloc(&pdev->dev, sizeof(struct max8925_backlight_data),
  118. GFP_KERNEL);
  119. if (data == NULL)
  120. return -ENOMEM;
  121. res = platform_get_resource(pdev, IORESOURCE_REG, 0);
  122. if (!res) {
  123. dev_err(&pdev->dev, "No REG resource for mode control!\n");
  124. return -ENXIO;
  125. }
  126. data->reg_mode_cntl = res->start;
  127. res = platform_get_resource(pdev, IORESOURCE_REG, 1);
  128. if (!res) {
  129. dev_err(&pdev->dev, "No REG resource for control!\n");
  130. return -ENXIO;
  131. }
  132. data->reg_cntl = res->start;
  133. data->chip = chip;
  134. data->current_brightness = 0;
  135. memset(&props, 0, sizeof(struct backlight_properties));
  136. props.type = BACKLIGHT_RAW;
  137. props.max_brightness = MAX_BRIGHTNESS;
  138. bl = devm_backlight_device_register(&pdev->dev, "max8925-backlight",
  139. &pdev->dev, data,
  140. &max8925_backlight_ops, &props);
  141. if (IS_ERR(bl)) {
  142. dev_err(&pdev->dev, "failed to register backlight\n");
  143. return PTR_ERR(bl);
  144. }
  145. bl->props.brightness = MAX_BRIGHTNESS;
  146. platform_set_drvdata(pdev, bl);
  147. value = 0;
  148. if (!pdev->dev.platform_data)
  149. max8925_backlight_dt_init(pdev);
  150. pdata = pdev->dev.platform_data;
  151. if (pdata) {
  152. if (pdata->lxw_scl)
  153. value |= (1 << 7);
  154. if (pdata->lxw_freq)
  155. value |= (LWX_FREQ(pdata->lxw_freq) << 4);
  156. if (pdata->dual_string)
  157. value |= (1 << 1);
  158. }
  159. ret = max8925_set_bits(chip->i2c, data->reg_mode_cntl, 0xfe, value);
  160. if (ret < 0)
  161. return ret;
  162. backlight_update_status(bl);
  163. return 0;
  164. }
  165. static struct platform_driver max8925_backlight_driver = {
  166. .driver = {
  167. .name = "max8925-backlight",
  168. },
  169. .probe = max8925_backlight_probe,
  170. };
  171. module_platform_driver(max8925_backlight_driver);
  172. MODULE_DESCRIPTION("Backlight Driver for Maxim MAX8925");
  173. MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
  174. MODULE_LICENSE("GPL");
  175. MODULE_ALIAS("platform:max8925-backlight");