blob: 515184fbe33a938ee0ae9645e0a9240de0a9563f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Laurent Pinchart67b43e52013-07-04 21:13:26 +02002/*
3 * ROHM Semiconductor BD6107 LED Driver
4 *
5 * Copyright (C) 2013 Ideas on board SPRL
6 *
7 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Laurent Pinchart67b43e52013-07-04 21:13:26 +02008 */
9
10#include <linux/backlight.h>
11#include <linux/delay.h>
12#include <linux/err.h>
13#include <linux/fb.h>
Linus Walleijc8fdcc82019-12-02 11:30:28 +010014#include <linux/gpio/consumer.h>
Laurent Pinchart67b43e52013-07-04 21:13:26 +020015#include <linux/i2c.h>
16#include <linux/module.h>
17#include <linux/platform_data/bd6107.h>
18#include <linux/slab.h>
19
20#define BD6107_PSCNT1 0x00
21#define BD6107_PSCNT1_PSCNTREG2 (1 << 2)
22#define BD6107_PSCNT1_PSCNTREG1 (1 << 0)
23#define BD6107_REGVSET 0x02
24#define BD6107_REGVSET_REG1VSET_2_85V (1 << 2)
25#define BD6107_REGVSET_REG1VSET_2_80V (0 << 2)
26#define BD6107_LEDCNT1 0x03
27#define BD6107_LEDCNT1_LEDONOFF2 (1 << 1)
28#define BD6107_LEDCNT1_LEDONOFF1 (1 << 0)
29#define BD6107_PORTSEL 0x04
30#define BD6107_PORTSEL_LEDM(n) (1 << (n))
31#define BD6107_RGB1CNT1 0x05
32#define BD6107_RGB1CNT2 0x06
33#define BD6107_RGB1CNT3 0x07
34#define BD6107_RGB1CNT4 0x08
35#define BD6107_RGB1CNT5 0x09
36#define BD6107_RGB1FLM 0x0a
37#define BD6107_RGB2CNT1 0x0b
38#define BD6107_RGB2CNT2 0x0c
39#define BD6107_RGB2CNT3 0x0d
40#define BD6107_RGB2CNT4 0x0e
41#define BD6107_RGB2CNT5 0x0f
42#define BD6107_RGB2FLM 0x10
43#define BD6107_PSCONT3 0x11
44#define BD6107_SMMONCNT 0x12
45#define BD6107_DCDCCNT 0x13
46#define BD6107_IOSEL 0x14
47#define BD6107_OUT1 0x15
48#define BD6107_OUT2 0x16
49#define BD6107_MASK1 0x17
50#define BD6107_MASK2 0x18
51#define BD6107_FACTOR1 0x19
52#define BD6107_FACTOR2 0x1a
53#define BD6107_CLRFACT1 0x1b
54#define BD6107_CLRFACT2 0x1c
55#define BD6107_STATE1 0x1d
56#define BD6107_LSIVER 0x1e
57#define BD6107_GRPSEL 0x1f
58#define BD6107_LEDCNT2 0x20
59#define BD6107_LEDCNT3 0x21
60#define BD6107_MCURRENT 0x22
61#define BD6107_MAINCNT1 0x23
62#define BD6107_MAINCNT2 0x24
63#define BD6107_SLOPECNT 0x25
64#define BD6107_MSLOPE 0x26
65#define BD6107_RGBSLOPE 0x27
66#define BD6107_TEST 0x29
67#define BD6107_SFTRST 0x2a
68#define BD6107_SFTRSTGD 0x2b
69
70struct bd6107 {
71 struct i2c_client *client;
72 struct backlight_device *backlight;
73 struct bd6107_platform_data *pdata;
Linus Walleijc8fdcc82019-12-02 11:30:28 +010074 struct gpio_desc *reset;
Laurent Pinchart67b43e52013-07-04 21:13:26 +020075};
76
77static int bd6107_write(struct bd6107 *bd, u8 reg, u8 data)
78{
79 return i2c_smbus_write_byte_data(bd->client, reg, data);
80}
81
82static int bd6107_backlight_update_status(struct backlight_device *backlight)
83{
84 struct bd6107 *bd = bl_get_data(backlight);
Sam Ravnborg51d53e52020-07-19 10:07:41 +020085 int brightness = backlight_get_brightness(backlight);
Laurent Pinchart67b43e52013-07-04 21:13:26 +020086
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 {
Linus Walleijc8fdcc82019-12-02 11:30:28 +010093 /* Assert the reset line (gpiolib will handle active low) */
94 gpiod_set_value(bd->reset, 1);
Laurent Pinchart67b43e52013-07-04 21:13:26 +020095 msleep(24);
Linus Walleijc8fdcc82019-12-02 11:30:28 +010096 gpiod_set_value(bd->reset, 0);
Laurent Pinchart67b43e52013-07-04 21:13:26 +020097 }
98
99 return 0;
100}
101
Laurent Pinchart67b43e52013-07-04 21:13:26 +0200102static int bd6107_backlight_check_fb(struct backlight_device *backlight,
103 struct fb_info *info)
104{
105 struct bd6107 *bd = bl_get_data(backlight);
106
107 return bd->pdata->fbdev == NULL || bd->pdata->fbdev == info->dev;
108}
109
110static const struct backlight_ops bd6107_backlight_ops = {
111 .options = BL_CORE_SUSPENDRESUME,
112 .update_status = bd6107_backlight_update_status,
Laurent Pinchart67b43e52013-07-04 21:13:26 +0200113 .check_fb = bd6107_backlight_check_fb,
114};
115
116static int bd6107_probe(struct i2c_client *client,
117 const struct i2c_device_id *id)
118{
Jingoo Hanc512794c2013-11-12 15:09:04 -0800119 struct bd6107_platform_data *pdata = dev_get_platdata(&client->dev);
Laurent Pinchart67b43e52013-07-04 21:13:26 +0200120 struct backlight_device *backlight;
121 struct backlight_properties props;
122 struct bd6107 *bd;
123 int ret;
124
Linus Walleijc8fdcc82019-12-02 11:30:28 +0100125 if (pdata == NULL) {
126 dev_err(&client->dev, "No platform data\n");
Laurent Pinchart67b43e52013-07-04 21:13:26 +0200127 return -EINVAL;
128 }
129
130 if (!i2c_check_functionality(client->adapter,
131 I2C_FUNC_SMBUS_BYTE_DATA)) {
132 dev_warn(&client->dev,
133 "I2C adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
134 return -EIO;
135 }
136
137 bd = devm_kzalloc(&client->dev, sizeof(*bd), GFP_KERNEL);
138 if (!bd)
139 return -ENOMEM;
140
141 bd->client = client;
142 bd->pdata = pdata;
143
Linus Walleijc8fdcc82019-12-02 11:30:28 +0100144 /*
145 * Request the reset GPIO line with GPIOD_OUT_HIGH meaning asserted,
146 * so in the machine descriptor table (or other hardware description),
147 * the line should be flagged as active low so this will assert
148 * the reset.
149 */
150 bd->reset = devm_gpiod_get(&client->dev, "reset", GPIOD_OUT_HIGH);
151 if (IS_ERR(bd->reset)) {
Laurent Pinchart67b43e52013-07-04 21:13:26 +0200152 dev_err(&client->dev, "unable to request reset GPIO\n");
Linus Walleijc8fdcc82019-12-02 11:30:28 +0100153 ret = PTR_ERR(bd->reset);
Laurent Pinchart67b43e52013-07-04 21:13:26 +0200154 return ret;
155 }
156
157 memset(&props, 0, sizeof(props));
158 props.type = BACKLIGHT_RAW;
159 props.max_brightness = 128;
160 props.brightness = clamp_t(unsigned int, pdata->def_value, 0,
161 props.max_brightness);
162
Jingoo Han26faf152013-11-12 15:09:12 -0800163 backlight = devm_backlight_device_register(&client->dev,
164 dev_name(&client->dev),
Laurent Pinchart67b43e52013-07-04 21:13:26 +0200165 &bd->client->dev, bd,
166 &bd6107_backlight_ops, &props);
167 if (IS_ERR(backlight)) {
168 dev_err(&client->dev, "failed to register backlight\n");
169 return PTR_ERR(backlight);
170 }
171
172 backlight_update_status(backlight);
173 i2c_set_clientdata(client, backlight);
174
175 return 0;
176}
177
178static int bd6107_remove(struct i2c_client *client)
179{
180 struct backlight_device *backlight = i2c_get_clientdata(client);
181
182 backlight->props.brightness = 0;
183 backlight_update_status(backlight);
Laurent Pinchart67b43e52013-07-04 21:13:26 +0200184
185 return 0;
186}
187
188static const struct i2c_device_id bd6107_ids[] = {
189 { "bd6107", 0 },
190 { }
191};
192MODULE_DEVICE_TABLE(i2c, bd6107_ids);
193
194static struct i2c_driver bd6107_driver = {
195 .driver = {
196 .name = "bd6107",
197 },
198 .probe = bd6107_probe,
199 .remove = bd6107_remove,
200 .id_table = bd6107_ids,
201};
202
203module_i2c_driver(bd6107_driver);
204
205MODULE_DESCRIPTION("Rohm BD6107 Backlight Driver");
206MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
207MODULE_LICENSE("GPL");