bd6107.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * ROHM Semiconductor BD6107 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/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/fb.h>
  16. #include <linux/gpio.h>
  17. #include <linux/i2c.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_data/bd6107.h>
  20. #include <linux/slab.h>
  21. #define BD6107_PSCNT1 0x00
  22. #define BD6107_PSCNT1_PSCNTREG2 (1 << 2)
  23. #define BD6107_PSCNT1_PSCNTREG1 (1 << 0)
  24. #define BD6107_REGVSET 0x02
  25. #define BD6107_REGVSET_REG1VSET_2_85V (1 << 2)
  26. #define BD6107_REGVSET_REG1VSET_2_80V (0 << 2)
  27. #define BD6107_LEDCNT1 0x03
  28. #define BD6107_LEDCNT1_LEDONOFF2 (1 << 1)
  29. #define BD6107_LEDCNT1_LEDONOFF1 (1 << 0)
  30. #define BD6107_PORTSEL 0x04
  31. #define BD6107_PORTSEL_LEDM(n) (1 << (n))
  32. #define BD6107_RGB1CNT1 0x05
  33. #define BD6107_RGB1CNT2 0x06
  34. #define BD6107_RGB1CNT3 0x07
  35. #define BD6107_RGB1CNT4 0x08
  36. #define BD6107_RGB1CNT5 0x09
  37. #define BD6107_RGB1FLM 0x0a
  38. #define BD6107_RGB2CNT1 0x0b
  39. #define BD6107_RGB2CNT2 0x0c
  40. #define BD6107_RGB2CNT3 0x0d
  41. #define BD6107_RGB2CNT4 0x0e
  42. #define BD6107_RGB2CNT5 0x0f
  43. #define BD6107_RGB2FLM 0x10
  44. #define BD6107_PSCONT3 0x11
  45. #define BD6107_SMMONCNT 0x12
  46. #define BD6107_DCDCCNT 0x13
  47. #define BD6107_IOSEL 0x14
  48. #define BD6107_OUT1 0x15
  49. #define BD6107_OUT2 0x16
  50. #define BD6107_MASK1 0x17
  51. #define BD6107_MASK2 0x18
  52. #define BD6107_FACTOR1 0x19
  53. #define BD6107_FACTOR2 0x1a
  54. #define BD6107_CLRFACT1 0x1b
  55. #define BD6107_CLRFACT2 0x1c
  56. #define BD6107_STATE1 0x1d
  57. #define BD6107_LSIVER 0x1e
  58. #define BD6107_GRPSEL 0x1f
  59. #define BD6107_LEDCNT2 0x20
  60. #define BD6107_LEDCNT3 0x21
  61. #define BD6107_MCURRENT 0x22
  62. #define BD6107_MAINCNT1 0x23
  63. #define BD6107_MAINCNT2 0x24
  64. #define BD6107_SLOPECNT 0x25
  65. #define BD6107_MSLOPE 0x26
  66. #define BD6107_RGBSLOPE 0x27
  67. #define BD6107_TEST 0x29
  68. #define BD6107_SFTRST 0x2a
  69. #define BD6107_SFTRSTGD 0x2b
  70. struct bd6107 {
  71. struct i2c_client *client;
  72. struct backlight_device *backlight;
  73. struct bd6107_platform_data *pdata;
  74. };
  75. static int bd6107_write(struct bd6107 *bd, u8 reg, u8 data)
  76. {
  77. return i2c_smbus_write_byte_data(bd->client, reg, data);
  78. }
  79. static int bd6107_backlight_update_status(struct backlight_device *backlight)
  80. {
  81. struct bd6107 *bd = bl_get_data(backlight);
  82. int brightness = backlight->props.brightness;
  83. if (backlight->props.power != FB_BLANK_UNBLANK ||
  84. backlight->props.fb_blank != FB_BLANK_UNBLANK ||
  85. backlight->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
  86. brightness = 0;
  87. if (brightness) {
  88. bd6107_write(bd, BD6107_PORTSEL, BD6107_PORTSEL_LEDM(2) |
  89. BD6107_PORTSEL_LEDM(1) | BD6107_PORTSEL_LEDM(0));
  90. bd6107_write(bd, BD6107_MAINCNT1, brightness);
  91. bd6107_write(bd, BD6107_LEDCNT1, BD6107_LEDCNT1_LEDONOFF1);
  92. } else {
  93. gpio_set_value(bd->pdata->reset, 0);
  94. msleep(24);
  95. gpio_set_value(bd->pdata->reset, 1);
  96. }
  97. return 0;
  98. }
  99. static int bd6107_backlight_check_fb(struct backlight_device *backlight,
  100. struct fb_info *info)
  101. {
  102. struct bd6107 *bd = bl_get_data(backlight);
  103. return bd->pdata->fbdev == NULL || bd->pdata->fbdev == info->dev;
  104. }
  105. static const struct backlight_ops bd6107_backlight_ops = {
  106. .options = BL_CORE_SUSPENDRESUME,
  107. .update_status = bd6107_backlight_update_status,
  108. .check_fb = bd6107_backlight_check_fb,
  109. };
  110. static int bd6107_probe(struct i2c_client *client,
  111. const struct i2c_device_id *id)
  112. {
  113. struct bd6107_platform_data *pdata = dev_get_platdata(&client->dev);
  114. struct backlight_device *backlight;
  115. struct backlight_properties props;
  116. struct bd6107 *bd;
  117. int ret;
  118. if (pdata == NULL || !pdata->reset) {
  119. dev_err(&client->dev, "No reset GPIO in platform data\n");
  120. return -EINVAL;
  121. }
  122. if (!i2c_check_functionality(client->adapter,
  123. I2C_FUNC_SMBUS_BYTE_DATA)) {
  124. dev_warn(&client->dev,
  125. "I2C adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
  126. return -EIO;
  127. }
  128. bd = devm_kzalloc(&client->dev, sizeof(*bd), GFP_KERNEL);
  129. if (!bd)
  130. return -ENOMEM;
  131. bd->client = client;
  132. bd->pdata = pdata;
  133. ret = devm_gpio_request_one(&client->dev, pdata->reset,
  134. GPIOF_DIR_OUT | GPIOF_INIT_LOW, "reset");
  135. if (ret < 0) {
  136. dev_err(&client->dev, "unable to request reset GPIO\n");
  137. return ret;
  138. }
  139. memset(&props, 0, sizeof(props));
  140. props.type = BACKLIGHT_RAW;
  141. props.max_brightness = 128;
  142. props.brightness = clamp_t(unsigned int, pdata->def_value, 0,
  143. props.max_brightness);
  144. backlight = devm_backlight_device_register(&client->dev,
  145. dev_name(&client->dev),
  146. &bd->client->dev, bd,
  147. &bd6107_backlight_ops, &props);
  148. if (IS_ERR(backlight)) {
  149. dev_err(&client->dev, "failed to register backlight\n");
  150. return PTR_ERR(backlight);
  151. }
  152. backlight_update_status(backlight);
  153. i2c_set_clientdata(client, backlight);
  154. return 0;
  155. }
  156. static int bd6107_remove(struct i2c_client *client)
  157. {
  158. struct backlight_device *backlight = i2c_get_clientdata(client);
  159. backlight->props.brightness = 0;
  160. backlight_update_status(backlight);
  161. return 0;
  162. }
  163. static const struct i2c_device_id bd6107_ids[] = {
  164. { "bd6107", 0 },
  165. { }
  166. };
  167. MODULE_DEVICE_TABLE(i2c, bd6107_ids);
  168. static struct i2c_driver bd6107_driver = {
  169. .driver = {
  170. .name = "bd6107",
  171. },
  172. .probe = bd6107_probe,
  173. .remove = bd6107_remove,
  174. .id_table = bd6107_ids,
  175. };
  176. module_i2c_driver(bd6107_driver);
  177. MODULE_DESCRIPTION("Rohm BD6107 Backlight Driver");
  178. MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
  179. MODULE_LICENSE("GPL");