lv5207lp.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Sanyo LV5207LP LED Driver
  3. *
  4. * Copyright (C) 2013 Ideas on board SPRL
  5. *
  6. * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/backlight.h>
  13. #include <linux/err.h>
  14. #include <linux/fb.h>
  15. #include <linux/i2c.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_data/lv5207lp.h>
  18. #include <linux/slab.h>
  19. #define LV5207LP_CTRL1 0x00
  20. #define LV5207LP_CPSW (1 << 7)
  21. #define LV5207LP_SCTEN (1 << 6)
  22. #define LV5207LP_C10 (1 << 5)
  23. #define LV5207LP_CKSW (1 << 4)
  24. #define LV5207LP_RSW (1 << 3)
  25. #define LV5207LP_GSW (1 << 2)
  26. #define LV5207LP_BSW (1 << 1)
  27. #define LV5207LP_CTRL2 0x01
  28. #define LV5207LP_MSW (1 << 7)
  29. #define LV5207LP_MLED4 (1 << 6)
  30. #define LV5207LP_RED 0x02
  31. #define LV5207LP_GREEN 0x03
  32. #define LV5207LP_BLUE 0x04
  33. #define LV5207LP_MAX_BRIGHTNESS 32
  34. struct lv5207lp {
  35. struct i2c_client *client;
  36. struct backlight_device *backlight;
  37. struct lv5207lp_platform_data *pdata;
  38. };
  39. static int lv5207lp_write(struct lv5207lp *lv, u8 reg, u8 data)
  40. {
  41. return i2c_smbus_write_byte_data(lv->client, reg, data);
  42. }
  43. static int lv5207lp_backlight_update_status(struct backlight_device *backlight)
  44. {
  45. struct lv5207lp *lv = bl_get_data(backlight);
  46. int brightness = backlight->props.brightness;
  47. if (backlight->props.power != FB_BLANK_UNBLANK ||
  48. backlight->props.fb_blank != FB_BLANK_UNBLANK ||
  49. backlight->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
  50. brightness = 0;
  51. if (brightness) {
  52. lv5207lp_write(lv, LV5207LP_CTRL1,
  53. LV5207LP_CPSW | LV5207LP_C10 | LV5207LP_CKSW);
  54. lv5207lp_write(lv, LV5207LP_CTRL2,
  55. LV5207LP_MSW | LV5207LP_MLED4 |
  56. (brightness - 1));
  57. } else {
  58. lv5207lp_write(lv, LV5207LP_CTRL1, 0);
  59. lv5207lp_write(lv, LV5207LP_CTRL2, 0);
  60. }
  61. return 0;
  62. }
  63. static int lv5207lp_backlight_check_fb(struct backlight_device *backlight,
  64. struct fb_info *info)
  65. {
  66. struct lv5207lp *lv = bl_get_data(backlight);
  67. return lv->pdata->fbdev == NULL || lv->pdata->fbdev == info->dev;
  68. }
  69. static const struct backlight_ops lv5207lp_backlight_ops = {
  70. .options = BL_CORE_SUSPENDRESUME,
  71. .update_status = lv5207lp_backlight_update_status,
  72. .check_fb = lv5207lp_backlight_check_fb,
  73. };
  74. static int lv5207lp_probe(struct i2c_client *client,
  75. const struct i2c_device_id *id)
  76. {
  77. struct lv5207lp_platform_data *pdata = dev_get_platdata(&client->dev);
  78. struct backlight_device *backlight;
  79. struct backlight_properties props;
  80. struct lv5207lp *lv;
  81. if (pdata == NULL) {
  82. dev_err(&client->dev, "No platform data supplied\n");
  83. return -EINVAL;
  84. }
  85. if (!i2c_check_functionality(client->adapter,
  86. I2C_FUNC_SMBUS_BYTE_DATA)) {
  87. dev_warn(&client->dev,
  88. "I2C adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
  89. return -EIO;
  90. }
  91. lv = devm_kzalloc(&client->dev, sizeof(*lv), GFP_KERNEL);
  92. if (!lv)
  93. return -ENOMEM;
  94. lv->client = client;
  95. lv->pdata = pdata;
  96. memset(&props, 0, sizeof(props));
  97. props.type = BACKLIGHT_RAW;
  98. props.max_brightness = min_t(unsigned int, pdata->max_value,
  99. LV5207LP_MAX_BRIGHTNESS);
  100. props.brightness = clamp_t(unsigned int, pdata->def_value, 0,
  101. props.max_brightness);
  102. backlight = devm_backlight_device_register(&client->dev,
  103. dev_name(&client->dev), &lv->client->dev,
  104. lv, &lv5207lp_backlight_ops, &props);
  105. if (IS_ERR(backlight)) {
  106. dev_err(&client->dev, "failed to register backlight\n");
  107. return PTR_ERR(backlight);
  108. }
  109. backlight_update_status(backlight);
  110. i2c_set_clientdata(client, backlight);
  111. return 0;
  112. }
  113. static int lv5207lp_remove(struct i2c_client *client)
  114. {
  115. struct backlight_device *backlight = i2c_get_clientdata(client);
  116. backlight->props.brightness = 0;
  117. backlight_update_status(backlight);
  118. return 0;
  119. }
  120. static const struct i2c_device_id lv5207lp_ids[] = {
  121. { "lv5207lp", 0 },
  122. { }
  123. };
  124. MODULE_DEVICE_TABLE(i2c, lv5207lp_ids);
  125. static struct i2c_driver lv5207lp_driver = {
  126. .driver = {
  127. .name = "lv5207lp",
  128. },
  129. .probe = lv5207lp_probe,
  130. .remove = lv5207lp_remove,
  131. .id_table = lv5207lp_ids,
  132. };
  133. module_i2c_driver(lv5207lp_driver);
  134. MODULE_DESCRIPTION("Sanyo LV5207LP Backlight Driver");
  135. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  136. MODULE_LICENSE("GPL");