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