1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * sky81452-backlight.c SKY81452 backlight driver 4 * 5 * Copyright 2014 Skyworks Solutions Inc. 6 * Author : Gyungoh Yoo <jack.yoo@skyworksinc.com> 7 */ 8 9 #include <linux/backlight.h> 10 #include <linux/err.h> 11 #include <linux/gpio.h> 12 #include <linux/init.h> 13 #include <linux/kernel.h> 14 #include <linux/module.h> 15 #include <linux/of.h> 16 #include <linux/of_gpio.h> 17 #include <linux/platform_device.h> 18 #include <linux/regmap.h> 19 #include <linux/platform_data/sky81452-backlight.h> 20 #include <linux/slab.h> 21 22 /* registers */ 23 #define SKY81452_REG0 0x00 24 #define SKY81452_REG1 0x01 25 #define SKY81452_REG2 0x02 26 #define SKY81452_REG4 0x04 27 #define SKY81452_REG5 0x05 28 29 /* bit mask */ 30 #define SKY81452_CS 0xFF 31 #define SKY81452_EN 0x3F 32 #define SKY81452_IGPW 0x20 33 #define SKY81452_PWMMD 0x10 34 #define SKY81452_PHASE 0x08 35 #define SKY81452_ILIM 0x04 36 #define SKY81452_VSHRT 0x03 37 #define SKY81452_OCP 0x80 38 #define SKY81452_OTMP 0x40 39 #define SKY81452_SHRT 0x3F 40 #define SKY81452_OPN 0x3F 41 42 #define SKY81452_DEFAULT_NAME "lcd-backlight" 43 #define SKY81452_MAX_BRIGHTNESS (SKY81452_CS + 1) 44 45 #define CTZ(b) __builtin_ctz(b) 46 47 static int sky81452_bl_update_status(struct backlight_device *bd) 48 { 49 const struct sky81452_bl_platform_data *pdata = 50 dev_get_platdata(bd->dev.parent); 51 const unsigned int brightness = (unsigned int)bd->props.brightness; 52 struct regmap *regmap = bl_get_data(bd); 53 int ret; 54 55 if (brightness > 0) { 56 ret = regmap_write(regmap, SKY81452_REG0, brightness - 1); 57 if (ret < 0) 58 return ret; 59 60 return regmap_update_bits(regmap, SKY81452_REG1, SKY81452_EN, 61 pdata->enable << CTZ(SKY81452_EN)); 62 } 63 64 return regmap_update_bits(regmap, SKY81452_REG1, SKY81452_EN, 0); 65 } 66 67 static const struct backlight_ops sky81452_bl_ops = { 68 .update_status = sky81452_bl_update_status, 69 }; 70 71 static ssize_t sky81452_bl_store_enable(struct device *dev, 72 struct device_attribute *attr, const char *buf, size_t count) 73 { 74 struct regmap *regmap = bl_get_data(to_backlight_device(dev)); 75 unsigned long value; 76 int ret; 77 78 ret = kstrtoul(buf, 16, &value); 79 if (ret < 0) 80 return ret; 81 82 ret = regmap_update_bits(regmap, SKY81452_REG1, SKY81452_EN, 83 value << CTZ(SKY81452_EN)); 84 if (ret < 0) 85 return ret; 86 87 return count; 88 } 89 90 static ssize_t sky81452_bl_show_open_short(struct device *dev, 91 struct device_attribute *attr, char *buf) 92 { 93 struct regmap *regmap = bl_get_data(to_backlight_device(dev)); 94 unsigned int reg, value = 0; 95 char tmp[3]; 96 int i, ret; 97 98 reg = !strcmp(attr->attr.name, "open") ? SKY81452_REG5 : SKY81452_REG4; 99 ret = regmap_read(regmap, reg, &value); 100 if (ret < 0) 101 return ret; 102 103 if (value & SKY81452_SHRT) { 104 *buf = 0; 105 for (i = 0; i < 6; i++) { 106 if (value & 0x01) { 107 sprintf(tmp, "%d ", i + 1); 108 strcat(buf, tmp); 109 } 110 value >>= 1; 111 } 112 strcat(buf, "\n"); 113 } else { 114 strcpy(buf, "none\n"); 115 } 116 117 return strlen(buf); 118 } 119 120 static ssize_t sky81452_bl_show_fault(struct device *dev, 121 struct device_attribute *attr, char *buf) 122 { 123 struct regmap *regmap = bl_get_data(to_backlight_device(dev)); 124 unsigned int value = 0; 125 int ret; 126 127 ret = regmap_read(regmap, SKY81452_REG4, &value); 128 if (ret < 0) 129 return ret; 130 131 *buf = 0; 132 133 if (value & SKY81452_OCP) 134 strcat(buf, "over-current "); 135 136 if (value & SKY81452_OTMP) 137 strcat(buf, "over-temperature"); 138 139 strcat(buf, "\n"); 140 return strlen(buf); 141 } 142 143 static DEVICE_ATTR(enable, S_IWGRP | S_IWUSR, NULL, sky81452_bl_store_enable); 144 static DEVICE_ATTR(open, S_IRUGO, sky81452_bl_show_open_short, NULL); 145 static DEVICE_ATTR(short, S_IRUGO, sky81452_bl_show_open_short, NULL); 146 static DEVICE_ATTR(fault, S_IRUGO, sky81452_bl_show_fault, NULL); 147 148 static struct attribute *sky81452_bl_attribute[] = { 149 &dev_attr_enable.attr, 150 &dev_attr_open.attr, 151 &dev_attr_short.attr, 152 &dev_attr_fault.attr, 153 NULL 154 }; 155 156 static const struct attribute_group sky81452_bl_attr_group = { 157 .attrs = sky81452_bl_attribute, 158 }; 159 160 #ifdef CONFIG_OF 161 static struct sky81452_bl_platform_data *sky81452_bl_parse_dt( 162 struct device *dev) 163 { 164 struct device_node *np = of_node_get(dev->of_node); 165 struct sky81452_bl_platform_data *pdata; 166 int num_entry; 167 unsigned int sources[6]; 168 int ret; 169 170 if (!np) { 171 dev_err(dev, "backlight node not found.\n"); 172 return ERR_PTR(-ENODATA); 173 } 174 175 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 176 if (!pdata) { 177 of_node_put(np); 178 return ERR_PTR(-ENOMEM); 179 } 180 181 of_property_read_string(np, "name", &pdata->name); 182 pdata->ignore_pwm = of_property_read_bool(np, "skyworks,ignore-pwm"); 183 pdata->dpwm_mode = of_property_read_bool(np, "skyworks,dpwm-mode"); 184 pdata->phase_shift = of_property_read_bool(np, "skyworks,phase-shift"); 185 pdata->gpio_enable = of_get_gpio(np, 0); 186 187 ret = of_property_count_u32_elems(np, "led-sources"); 188 if (ret < 0) { 189 pdata->enable = SKY81452_EN >> CTZ(SKY81452_EN); 190 } else { 191 num_entry = ret; 192 if (num_entry > 6) 193 num_entry = 6; 194 195 ret = of_property_read_u32_array(np, "led-sources", sources, 196 num_entry); 197 if (ret < 0) { 198 dev_err(dev, "led-sources node is invalid.\n"); 199 return ERR_PTR(-EINVAL); 200 } 201 202 pdata->enable = 0; 203 while (--num_entry) 204 pdata->enable |= (1 << sources[num_entry]); 205 } 206 207 ret = of_property_read_u32(np, 208 "skyworks,short-detection-threshold-volt", 209 &pdata->short_detection_threshold); 210 if (ret < 0) 211 pdata->short_detection_threshold = 7; 212 213 ret = of_property_read_u32(np, "skyworks,current-limit-mA", 214 &pdata->boost_current_limit); 215 if (ret < 0) 216 pdata->boost_current_limit = 2750; 217 218 of_node_put(np); 219 return pdata; 220 } 221 #else 222 static struct sky81452_bl_platform_data *sky81452_bl_parse_dt( 223 struct device *dev) 224 { 225 return ERR_PTR(-EINVAL); 226 } 227 #endif 228 229 static int sky81452_bl_init_device(struct regmap *regmap, 230 struct sky81452_bl_platform_data *pdata) 231 { 232 unsigned int value; 233 234 value = pdata->ignore_pwm ? SKY81452_IGPW : 0; 235 value |= pdata->dpwm_mode ? SKY81452_PWMMD : 0; 236 value |= pdata->phase_shift ? 0 : SKY81452_PHASE; 237 238 if (pdata->boost_current_limit == 2300) 239 value |= SKY81452_ILIM; 240 else if (pdata->boost_current_limit != 2750) 241 return -EINVAL; 242 243 if (pdata->short_detection_threshold < 4 || 244 pdata->short_detection_threshold > 7) 245 return -EINVAL; 246 value |= (7 - pdata->short_detection_threshold) << CTZ(SKY81452_VSHRT); 247 248 return regmap_write(regmap, SKY81452_REG2, value); 249 } 250 251 static int sky81452_bl_probe(struct platform_device *pdev) 252 { 253 struct device *dev = &pdev->dev; 254 struct regmap *regmap = dev_get_drvdata(dev->parent); 255 struct sky81452_bl_platform_data *pdata = dev_get_platdata(dev); 256 struct backlight_device *bd; 257 struct backlight_properties props; 258 const char *name; 259 int ret; 260 261 if (!pdata) { 262 pdata = sky81452_bl_parse_dt(dev); 263 if (IS_ERR(pdata)) 264 return PTR_ERR(pdata); 265 } 266 267 if (gpio_is_valid(pdata->gpio_enable)) { 268 ret = devm_gpio_request_one(dev, pdata->gpio_enable, 269 GPIOF_OUT_INIT_HIGH, "sky81452-en"); 270 if (ret < 0) { 271 dev_err(dev, "failed to request GPIO. err=%d\n", ret); 272 return ret; 273 } 274 } 275 276 ret = sky81452_bl_init_device(regmap, pdata); 277 if (ret < 0) { 278 dev_err(dev, "failed to initialize. err=%d\n", ret); 279 return ret; 280 } 281 282 memset(&props, 0, sizeof(props)); 283 props.max_brightness = SKY81452_MAX_BRIGHTNESS, 284 name = pdata->name ? pdata->name : SKY81452_DEFAULT_NAME; 285 bd = devm_backlight_device_register(dev, name, dev, regmap, 286 &sky81452_bl_ops, &props); 287 if (IS_ERR(bd)) { 288 dev_err(dev, "failed to register. err=%ld\n", PTR_ERR(bd)); 289 return PTR_ERR(bd); 290 } 291 292 platform_set_drvdata(pdev, bd); 293 294 ret = sysfs_create_group(&bd->dev.kobj, &sky81452_bl_attr_group); 295 if (ret < 0) { 296 dev_err(dev, "failed to create attribute. err=%d\n", ret); 297 return ret; 298 } 299 300 return ret; 301 } 302 303 static int sky81452_bl_remove(struct platform_device *pdev) 304 { 305 const struct sky81452_bl_platform_data *pdata = 306 dev_get_platdata(&pdev->dev); 307 struct backlight_device *bd = platform_get_drvdata(pdev); 308 309 sysfs_remove_group(&bd->dev.kobj, &sky81452_bl_attr_group); 310 311 bd->props.power = FB_BLANK_UNBLANK; 312 bd->props.brightness = 0; 313 backlight_update_status(bd); 314 315 if (gpio_is_valid(pdata->gpio_enable)) 316 gpio_set_value_cansleep(pdata->gpio_enable, 0); 317 318 return 0; 319 } 320 321 #ifdef CONFIG_OF 322 static const struct of_device_id sky81452_bl_of_match[] = { 323 { .compatible = "skyworks,sky81452-backlight", }, 324 { } 325 }; 326 MODULE_DEVICE_TABLE(of, sky81452_bl_of_match); 327 #endif 328 329 static struct platform_driver sky81452_bl_driver = { 330 .driver = { 331 .name = "sky81452-backlight", 332 .of_match_table = of_match_ptr(sky81452_bl_of_match), 333 }, 334 .probe = sky81452_bl_probe, 335 .remove = sky81452_bl_remove, 336 }; 337 338 module_platform_driver(sky81452_bl_driver); 339 340 MODULE_DESCRIPTION("Skyworks SKY81452 backlight driver"); 341 MODULE_AUTHOR("Gyungoh Yoo <jack.yoo@skyworksinc.com>"); 342 MODULE_LICENSE("GPL v2"); 343