xref: /openbmc/linux/drivers/video/backlight/bd6107.c (revision 8730046c)
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_check_fb(struct backlight_device *backlight,
109 				       struct fb_info *info)
110 {
111 	struct bd6107 *bd = bl_get_data(backlight);
112 
113 	return bd->pdata->fbdev == NULL || bd->pdata->fbdev == info->dev;
114 }
115 
116 static const struct backlight_ops bd6107_backlight_ops = {
117 	.options	= BL_CORE_SUSPENDRESUME,
118 	.update_status	= bd6107_backlight_update_status,
119 	.check_fb	= bd6107_backlight_check_fb,
120 };
121 
122 static int bd6107_probe(struct i2c_client *client,
123 			  const struct i2c_device_id *id)
124 {
125 	struct bd6107_platform_data *pdata = dev_get_platdata(&client->dev);
126 	struct backlight_device *backlight;
127 	struct backlight_properties props;
128 	struct bd6107 *bd;
129 	int ret;
130 
131 	if (pdata == NULL || !pdata->reset) {
132 		dev_err(&client->dev, "No reset GPIO in platform data\n");
133 		return -EINVAL;
134 	}
135 
136 	if (!i2c_check_functionality(client->adapter,
137 				     I2C_FUNC_SMBUS_BYTE_DATA)) {
138 		dev_warn(&client->dev,
139 			 "I2C adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
140 		return -EIO;
141 	}
142 
143 	bd = devm_kzalloc(&client->dev, sizeof(*bd), GFP_KERNEL);
144 	if (!bd)
145 		return -ENOMEM;
146 
147 	bd->client = client;
148 	bd->pdata = pdata;
149 
150 	ret = devm_gpio_request_one(&client->dev, pdata->reset,
151 				    GPIOF_DIR_OUT | GPIOF_INIT_LOW, "reset");
152 	if (ret < 0) {
153 		dev_err(&client->dev, "unable to request reset GPIO\n");
154 		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 
163 	backlight = devm_backlight_device_register(&client->dev,
164 					      dev_name(&client->dev),
165 					      &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 
178 static 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);
184 
185 	return 0;
186 }
187 
188 static const struct i2c_device_id bd6107_ids[] = {
189 	{ "bd6107", 0 },
190 	{ }
191 };
192 MODULE_DEVICE_TABLE(i2c, bd6107_ids);
193 
194 static 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 
203 module_i2c_driver(bd6107_driver);
204 
205 MODULE_DESCRIPTION("Rohm BD6107 Backlight Driver");
206 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
207 MODULE_LICENSE("GPL");
208