88pm860x_bl.c (99dbb1632f1165c2726056ebfce6edde0e5a0208) | 88pm860x_bl.c (2e57d56747e601b3e0ff6697e524025d0504d161) |
---|---|
1/* 2 * Backlight driver for Marvell Semiconductor 88PM8606 3 * 4 * Copyright (C) 2009 Marvell International Ltd. 5 * Haojian Zhuang <haojian.zhuang@marvell.com> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 */ 11 12#include <linux/init.h> 13#include <linux/kernel.h> | 1/* 2 * Backlight driver for Marvell Semiconductor 88PM8606 3 * 4 * Copyright (C) 2009 Marvell International Ltd. 5 * Haojian Zhuang <haojian.zhuang@marvell.com> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 */ 11 12#include <linux/init.h> 13#include <linux/kernel.h> |
14#include <linux/of.h> |
|
14#include <linux/platform_device.h> 15#include <linux/slab.h> 16#include <linux/fb.h> 17#include <linux/i2c.h> 18#include <linux/backlight.h> 19#include <linux/mfd/88pm860x.h> 20#include <linux/module.h> 21 --- 4 unchanged lines hidden (view full) --- 26 27struct pm860x_backlight_data { 28 struct pm860x_chip *chip; 29 struct i2c_client *i2c; 30 int current_brightness; 31 int port; 32 int pwm; 33 int iset; | 15#include <linux/platform_device.h> 16#include <linux/slab.h> 17#include <linux/fb.h> 18#include <linux/i2c.h> 19#include <linux/backlight.h> 20#include <linux/mfd/88pm860x.h> 21#include <linux/module.h> 22 --- 4 unchanged lines hidden (view full) --- 27 28struct pm860x_backlight_data { 29 struct pm860x_chip *chip; 30 struct i2c_client *i2c; 31 int current_brightness; 32 int port; 33 int pwm; 34 int iset; |
35 int reg_duty_cycle; 36 int reg_always_on; 37 int reg_current; |
|
34}; 35 | 38}; 39 |
36static inline int wled_a(int port) 37{ 38 int ret; 39 40 ret = ((port - PM8606_BACKLIGHT1) << 1) + 2; 41 return ret; 42} 43 44static inline int wled_b(int port) 45{ 46 int ret; 47 48 ret = ((port - PM8606_BACKLIGHT1) << 1) + 3; 49 return ret; 50} 51 52/* WLED2 & WLED3 share the same IDC */ 53static inline int wled_idc(int port) 54{ 55 int ret; 56 57 switch (port) { 58 case PM8606_BACKLIGHT1: 59 case PM8606_BACKLIGHT2: 60 ret = ((port - PM8606_BACKLIGHT1) << 1) + 3; 61 break; 62 case PM8606_BACKLIGHT3: 63 default: 64 ret = ((port - PM8606_BACKLIGHT2) << 1) + 3; 65 break; 66 } 67 return ret; 68} 69 | |
70static int backlight_power_set(struct pm860x_chip *chip, int port, 71 int on) 72{ 73 int ret = -EINVAL; 74 75 switch (port) { | 40static int backlight_power_set(struct pm860x_chip *chip, int port, 41 int on) 42{ 43 int ret = -EINVAL; 44 45 switch (port) { |
76 case PM8606_BACKLIGHT1: | 46 case 0: |
77 ret = on ? pm8606_osc_enable(chip, WLED1_DUTY) : 78 pm8606_osc_disable(chip, WLED1_DUTY); 79 break; | 47 ret = on ? pm8606_osc_enable(chip, WLED1_DUTY) : 48 pm8606_osc_disable(chip, WLED1_DUTY); 49 break; |
80 case PM8606_BACKLIGHT2: | 50 case 1: |
81 ret = on ? pm8606_osc_enable(chip, WLED2_DUTY) : 82 pm8606_osc_disable(chip, WLED2_DUTY); 83 break; | 51 ret = on ? pm8606_osc_enable(chip, WLED2_DUTY) : 52 pm8606_osc_disable(chip, WLED2_DUTY); 53 break; |
84 case PM8606_BACKLIGHT3: | 54 case 2: |
85 ret = on ? pm8606_osc_enable(chip, WLED3_DUTY) : 86 pm8606_osc_disable(chip, WLED3_DUTY); 87 break; 88 } 89 return ret; 90} 91 92static int pm860x_backlight_set(struct backlight_device *bl, int brightness) --- 6 unchanged lines hidden (view full) --- 99 if (brightness > MAX_BRIGHTNESS) 100 value = MAX_BRIGHTNESS; 101 else 102 value = brightness; 103 104 if (brightness) 105 backlight_power_set(chip, data->port, 1); 106 | 55 ret = on ? pm8606_osc_enable(chip, WLED3_DUTY) : 56 pm8606_osc_disable(chip, WLED3_DUTY); 57 break; 58 } 59 return ret; 60} 61 62static int pm860x_backlight_set(struct backlight_device *bl, int brightness) --- 6 unchanged lines hidden (view full) --- 69 if (brightness > MAX_BRIGHTNESS) 70 value = MAX_BRIGHTNESS; 71 else 72 value = brightness; 73 74 if (brightness) 75 backlight_power_set(chip, data->port, 1); 76 |
107 ret = pm860x_reg_write(data->i2c, wled_a(data->port), value); | 77 ret = pm860x_reg_write(data->i2c, data->reg_duty_cycle, value); |
108 if (ret < 0) 109 goto out; 110 111 if ((data->current_brightness == 0) && brightness) { 112 if (data->iset) { | 78 if (ret < 0) 79 goto out; 80 81 if ((data->current_brightness == 0) && brightness) { 82 if (data->iset) { |
113 ret = pm860x_set_bits(data->i2c, wled_idc(data->port), | 83 ret = pm860x_set_bits(data->i2c, data->reg_current, |
114 CURRENT_BITMASK, data->iset); 115 if (ret < 0) 116 goto out; 117 } 118 if (data->pwm) { 119 ret = pm860x_set_bits(data->i2c, PM8606_PWM, 120 PM8606_PWM_FREQ_MASK, data->pwm); 121 if (ret < 0) 122 goto out; 123 } 124 if (brightness == MAX_BRIGHTNESS) { 125 /* set WLED_ON bit as 100% */ | 84 CURRENT_BITMASK, data->iset); 85 if (ret < 0) 86 goto out; 87 } 88 if (data->pwm) { 89 ret = pm860x_set_bits(data->i2c, PM8606_PWM, 90 PM8606_PWM_FREQ_MASK, data->pwm); 91 if (ret < 0) 92 goto out; 93 } 94 if (brightness == MAX_BRIGHTNESS) { 95 /* set WLED_ON bit as 100% */ |
126 ret = pm860x_set_bits(data->i2c, wled_b(data->port), | 96 ret = pm860x_set_bits(data->i2c, data->reg_always_on, |
127 PM8606_WLED_ON, PM8606_WLED_ON); 128 } 129 } else { 130 if (brightness == MAX_BRIGHTNESS) { 131 /* set WLED_ON bit as 100% */ | 97 PM8606_WLED_ON, PM8606_WLED_ON); 98 } 99 } else { 100 if (brightness == MAX_BRIGHTNESS) { 101 /* set WLED_ON bit as 100% */ |
132 ret = pm860x_set_bits(data->i2c, wled_b(data->port), | 102 ret = pm860x_set_bits(data->i2c, data->reg_always_on, |
133 PM8606_WLED_ON, PM8606_WLED_ON); 134 } else { 135 /* clear WLED_ON bit since it's not 100% */ | 103 PM8606_WLED_ON, PM8606_WLED_ON); 104 } else { 105 /* clear WLED_ON bit since it's not 100% */ |
136 ret = pm860x_set_bits(data->i2c, wled_b(data->port), | 106 ret = pm860x_set_bits(data->i2c, data->reg_always_on, |
137 PM8606_WLED_ON, 0); 138 } 139 } 140 if (ret < 0) 141 goto out; 142 143 if (brightness == 0) 144 backlight_power_set(chip, data->port, 0); --- 24 unchanged lines hidden (view full) --- 169} 170 171static int pm860x_backlight_get_brightness(struct backlight_device *bl) 172{ 173 struct pm860x_backlight_data *data = bl_get_data(bl); 174 struct pm860x_chip *chip = data->chip; 175 int ret; 176 | 107 PM8606_WLED_ON, 0); 108 } 109 } 110 if (ret < 0) 111 goto out; 112 113 if (brightness == 0) 114 backlight_power_set(chip, data->port, 0); --- 24 unchanged lines hidden (view full) --- 139} 140 141static int pm860x_backlight_get_brightness(struct backlight_device *bl) 142{ 143 struct pm860x_backlight_data *data = bl_get_data(bl); 144 struct pm860x_chip *chip = data->chip; 145 int ret; 146 |
177 ret = pm860x_reg_read(data->i2c, wled_a(data->port)); | 147 ret = pm860x_reg_read(data->i2c, data->reg_duty_cycle); |
178 if (ret < 0) 179 goto out; 180 data->current_brightness = ret; 181 dev_dbg(chip->dev, "get brightness %d\n", data->current_brightness); 182 return data->current_brightness; 183out: 184 return -EINVAL; 185} 186 187static const struct backlight_ops pm860x_backlight_ops = { 188 .options = BL_CORE_SUSPENDRESUME, 189 .update_status = pm860x_backlight_update_status, 190 .get_brightness = pm860x_backlight_get_brightness, 191}; 192 | 148 if (ret < 0) 149 goto out; 150 data->current_brightness = ret; 151 dev_dbg(chip->dev, "get brightness %d\n", data->current_brightness); 152 return data->current_brightness; 153out: 154 return -EINVAL; 155} 156 157static const struct backlight_ops pm860x_backlight_ops = { 158 .options = BL_CORE_SUSPENDRESUME, 159 .update_status = pm860x_backlight_update_status, 160 .get_brightness = pm860x_backlight_get_brightness, 161}; 162 |
163#ifdef CONFIG_OF 164static int pm860x_backlight_dt_init(struct platform_device *pdev, 165 struct pm860x_backlight_data *data, 166 char *name) 167{ 168 struct device_node *nproot = pdev->dev.parent->of_node, *np; 169 int iset = 0; 170 if (!nproot) 171 return -ENODEV; 172 nproot = of_find_node_by_name(nproot, "backlights"); 173 if (!nproot) { 174 dev_err(&pdev->dev, "failed to find backlights node\n"); 175 return -ENODEV; 176 } 177 for_each_child_of_node(nproot, np) { 178 if (!of_node_cmp(np->name, name)) { 179 of_property_read_u32(np, "marvell,88pm860x-iset", 180 &iset); 181 data->iset = PM8606_WLED_CURRENT(iset); 182 of_property_read_u32(np, "marvell,88pm860x-pwm", 183 &data->pwm); 184 break; 185 } 186 } 187 return 0; 188} 189#else 190#define pm860x_backlight_dt_init(x, y, z) (-1) 191#endif 192 |
|
193static int pm860x_backlight_probe(struct platform_device *pdev) 194{ 195 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); | 193static int pm860x_backlight_probe(struct platform_device *pdev) 194{ 195 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); |
196 struct pm860x_backlight_pdata *pdata = NULL; | 196 struct pm860x_backlight_pdata *pdata = pdev->dev.platform_data; |
197 struct pm860x_backlight_data *data; 198 struct backlight_device *bl; 199 struct resource *res; 200 struct backlight_properties props; 201 char name[MFD_NAME_SIZE]; | 197 struct pm860x_backlight_data *data; 198 struct backlight_device *bl; 199 struct resource *res; 200 struct backlight_properties props; 201 char name[MFD_NAME_SIZE]; |
202 int ret; | 202 int ret = 0; |
203 | 203 |
204 res = platform_get_resource(pdev, IORESOURCE_IO, 0); 205 if (res == NULL) { 206 dev_err(&pdev->dev, "No I/O resource!\n"); 207 return -EINVAL; 208 } 209 210 pdata = pdev->dev.platform_data; 211 if (pdata == NULL) { 212 dev_err(&pdev->dev, "platform data isn't assigned to " 213 "backlight\n"); 214 return -EINVAL; 215 } 216 | |
217 data = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_backlight_data), 218 GFP_KERNEL); 219 if (data == NULL) 220 return -ENOMEM; | 204 data = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_backlight_data), 205 GFP_KERNEL); 206 if (data == NULL) 207 return -ENOMEM; |
221 strncpy(name, res->name, MFD_NAME_SIZE); | 208 res = platform_get_resource_byname(pdev, IORESOURCE_REG, "duty cycle"); 209 if (!res) { 210 dev_err(&pdev->dev, "No REG resource for duty cycle\n"); 211 ret = -ENXIO; 212 goto out; 213 } 214 data->reg_duty_cycle = res->start; 215 res = platform_get_resource_byname(pdev, IORESOURCE_REG, "always on"); 216 if (!res) { 217 dev_err(&pdev->dev, "No REG resorce for always on\n"); 218 ret = -ENXIO; 219 goto out; 220 } 221 data->reg_always_on = res->start; 222 res = platform_get_resource_byname(pdev, IORESOURCE_REG, "current"); 223 if (!res) { 224 dev_err(&pdev->dev, "No REG resource for current\n"); 225 ret = -ENXIO; 226 goto out; 227 } 228 data->reg_current = res->start; 229 230 memset(name, 0, MFD_NAME_SIZE); 231 sprintf(name, "backlight-%d", pdev->id); 232 data->port = pdev->id; |
222 data->chip = chip; 223 data->i2c = (chip->id == CHIP_PM8606) ? chip->client \ 224 : chip->companion; 225 data->current_brightness = MAX_BRIGHTNESS; | 233 data->chip = chip; 234 data->i2c = (chip->id == CHIP_PM8606) ? chip->client \ 235 : chip->companion; 236 data->current_brightness = MAX_BRIGHTNESS; |
226 data->pwm = pdata->pwm; 227 data->iset = pdata->iset; 228 data->port = pdata->flags; 229 if (data->port < 0) { 230 dev_err(&pdev->dev, "wrong platform data is assigned"); 231 return -EINVAL; | 237 if (pm860x_backlight_dt_init(pdev, data, name)) { 238 if (pdata) { 239 data->pwm = pdata->pwm; 240 data->iset = pdata->iset; 241 } |
232 } 233 234 memset(&props, 0, sizeof(struct backlight_properties)); 235 props.type = BACKLIGHT_RAW; 236 props.max_brightness = MAX_BRIGHTNESS; 237 bl = backlight_device_register(name, &pdev->dev, data, 238 &pm860x_backlight_ops, &props); 239 if (IS_ERR(bl)) { 240 dev_err(&pdev->dev, "failed to register backlight\n"); 241 return PTR_ERR(bl); 242 } 243 bl->props.brightness = MAX_BRIGHTNESS; 244 245 platform_set_drvdata(pdev, bl); 246 247 /* read current backlight */ 248 ret = pm860x_backlight_get_brightness(bl); 249 if (ret < 0) | 242 } 243 244 memset(&props, 0, sizeof(struct backlight_properties)); 245 props.type = BACKLIGHT_RAW; 246 props.max_brightness = MAX_BRIGHTNESS; 247 bl = backlight_device_register(name, &pdev->dev, data, 248 &pm860x_backlight_ops, &props); 249 if (IS_ERR(bl)) { 250 dev_err(&pdev->dev, "failed to register backlight\n"); 251 return PTR_ERR(bl); 252 } 253 bl->props.brightness = MAX_BRIGHTNESS; 254 255 platform_set_drvdata(pdev, bl); 256 257 /* read current backlight */ 258 ret = pm860x_backlight_get_brightness(bl); 259 if (ret < 0) |
250 goto out; | 260 goto out_brt; |
251 252 backlight_update_status(bl); 253 return 0; | 261 262 backlight_update_status(bl); 263 return 0; |
254out: | 264out_brt: |
255 backlight_device_unregister(bl); | 265 backlight_device_unregister(bl); |
266out: 267 devm_kfree(&pdev->dev, data); |
|
256 return ret; 257} 258 259static int pm860x_backlight_remove(struct platform_device *pdev) 260{ 261 struct backlight_device *bl = platform_get_drvdata(pdev); 262 263 backlight_device_unregister(bl); --- 18 unchanged lines hidden --- | 268 return ret; 269} 270 271static int pm860x_backlight_remove(struct platform_device *pdev) 272{ 273 struct backlight_device *bl = platform_get_drvdata(pdev); 274 275 backlight_device_unregister(bl); --- 18 unchanged lines hidden --- |