Linux 4.18.10
[linux/fpc-iii.git] / drivers / video / backlight / lv5207lp.c
blob8ab7297b118ae2fced4620a43519db2bec4d89f6
1 /*
2 * Sanyo LV5207LP LED Driver
4 * Copyright (C) 2013 Ideas on board SPRL
6 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
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.
13 #include <linux/backlight.h>
14 #include <linux/err.h>
15 #include <linux/fb.h>
16 #include <linux/i2c.h>
17 #include <linux/module.h>
18 #include <linux/platform_data/lv5207lp.h>
19 #include <linux/slab.h>
21 #define LV5207LP_CTRL1 0x00
22 #define LV5207LP_CPSW (1 << 7)
23 #define LV5207LP_SCTEN (1 << 6)
24 #define LV5207LP_C10 (1 << 5)
25 #define LV5207LP_CKSW (1 << 4)
26 #define LV5207LP_RSW (1 << 3)
27 #define LV5207LP_GSW (1 << 2)
28 #define LV5207LP_BSW (1 << 1)
29 #define LV5207LP_CTRL2 0x01
30 #define LV5207LP_MSW (1 << 7)
31 #define LV5207LP_MLED4 (1 << 6)
32 #define LV5207LP_RED 0x02
33 #define LV5207LP_GREEN 0x03
34 #define LV5207LP_BLUE 0x04
36 #define LV5207LP_MAX_BRIGHTNESS 32
38 struct lv5207lp {
39 struct i2c_client *client;
40 struct backlight_device *backlight;
41 struct lv5207lp_platform_data *pdata;
44 static int lv5207lp_write(struct lv5207lp *lv, u8 reg, u8 data)
46 return i2c_smbus_write_byte_data(lv->client, reg, data);
49 static int lv5207lp_backlight_update_status(struct backlight_device *backlight)
51 struct lv5207lp *lv = bl_get_data(backlight);
52 int brightness = backlight->props.brightness;
54 if (backlight->props.power != FB_BLANK_UNBLANK ||
55 backlight->props.fb_blank != FB_BLANK_UNBLANK ||
56 backlight->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK))
57 brightness = 0;
59 if (brightness) {
60 lv5207lp_write(lv, LV5207LP_CTRL1,
61 LV5207LP_CPSW | LV5207LP_C10 | LV5207LP_CKSW);
62 lv5207lp_write(lv, LV5207LP_CTRL2,
63 LV5207LP_MSW | LV5207LP_MLED4 |
64 (brightness - 1));
65 } else {
66 lv5207lp_write(lv, LV5207LP_CTRL1, 0);
67 lv5207lp_write(lv, LV5207LP_CTRL2, 0);
70 return 0;
73 static int lv5207lp_backlight_check_fb(struct backlight_device *backlight,
74 struct fb_info *info)
76 struct lv5207lp *lv = bl_get_data(backlight);
78 return lv->pdata->fbdev == NULL || lv->pdata->fbdev == info->dev;
81 static const struct backlight_ops lv5207lp_backlight_ops = {
82 .options = BL_CORE_SUSPENDRESUME,
83 .update_status = lv5207lp_backlight_update_status,
84 .check_fb = lv5207lp_backlight_check_fb,
87 static int lv5207lp_probe(struct i2c_client *client,
88 const struct i2c_device_id *id)
90 struct lv5207lp_platform_data *pdata = dev_get_platdata(&client->dev);
91 struct backlight_device *backlight;
92 struct backlight_properties props;
93 struct lv5207lp *lv;
95 if (pdata == NULL) {
96 dev_err(&client->dev, "No platform data supplied\n");
97 return -EINVAL;
100 if (!i2c_check_functionality(client->adapter,
101 I2C_FUNC_SMBUS_BYTE_DATA)) {
102 dev_warn(&client->dev,
103 "I2C adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
104 return -EIO;
107 lv = devm_kzalloc(&client->dev, sizeof(*lv), GFP_KERNEL);
108 if (!lv)
109 return -ENOMEM;
111 lv->client = client;
112 lv->pdata = pdata;
114 memset(&props, 0, sizeof(props));
115 props.type = BACKLIGHT_RAW;
116 props.max_brightness = min_t(unsigned int, pdata->max_value,
117 LV5207LP_MAX_BRIGHTNESS);
118 props.brightness = clamp_t(unsigned int, pdata->def_value, 0,
119 props.max_brightness);
121 backlight = devm_backlight_device_register(&client->dev,
122 dev_name(&client->dev), &lv->client->dev,
123 lv, &lv5207lp_backlight_ops, &props);
124 if (IS_ERR(backlight)) {
125 dev_err(&client->dev, "failed to register backlight\n");
126 return PTR_ERR(backlight);
129 backlight_update_status(backlight);
130 i2c_set_clientdata(client, backlight);
132 return 0;
135 static int lv5207lp_remove(struct i2c_client *client)
137 struct backlight_device *backlight = i2c_get_clientdata(client);
139 backlight->props.brightness = 0;
140 backlight_update_status(backlight);
142 return 0;
145 static const struct i2c_device_id lv5207lp_ids[] = {
146 { "lv5207lp", 0 },
149 MODULE_DEVICE_TABLE(i2c, lv5207lp_ids);
151 static struct i2c_driver lv5207lp_driver = {
152 .driver = {
153 .name = "lv5207lp",
155 .probe = lv5207lp_probe,
156 .remove = lv5207lp_remove,
157 .id_table = lv5207lp_ids,
160 module_i2c_driver(lv5207lp_driver);
162 MODULE_DESCRIPTION("Sanyo LV5207LP Backlight Driver");
163 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
164 MODULE_LICENSE("GPL");