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