Baolin Wang | e081c49 | 2018-05-14 14:34:51 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // Copyright (C) 2018 Spreadtrum Communications Inc. |
| 3 | |
| 4 | #include <linux/leds.h> |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/of.h> |
| 7 | #include <linux/platform_device.h> |
| 8 | #include <linux/regmap.h> |
| 9 | #include <uapi/linux/uleds.h> |
| 10 | |
| 11 | /* PMIC global control register definition */ |
| 12 | #define SC27XX_MODULE_EN0 0xc08 |
| 13 | #define SC27XX_CLK_EN0 0xc18 |
| 14 | #define SC27XX_RGB_CTRL 0xebc |
| 15 | |
| 16 | #define SC27XX_BLTC_EN BIT(9) |
| 17 | #define SC27XX_RTC_EN BIT(7) |
| 18 | #define SC27XX_RGB_PD BIT(0) |
| 19 | |
| 20 | /* Breathing light controller register definition */ |
| 21 | #define SC27XX_LEDS_CTRL 0x00 |
| 22 | #define SC27XX_LEDS_PRESCALE 0x04 |
| 23 | #define SC27XX_LEDS_DUTY 0x08 |
| 24 | #define SC27XX_LEDS_CURVE0 0x0c |
| 25 | #define SC27XX_LEDS_CURVE1 0x10 |
| 26 | |
| 27 | #define SC27XX_CTRL_SHIFT 4 |
| 28 | #define SC27XX_LED_RUN BIT(0) |
| 29 | #define SC27XX_LED_TYPE BIT(1) |
| 30 | |
| 31 | #define SC27XX_DUTY_SHIFT 8 |
| 32 | #define SC27XX_DUTY_MASK GENMASK(15, 0) |
| 33 | #define SC27XX_MOD_MASK GENMASK(7, 0) |
| 34 | |
Baolin Wang | 8dbac65 | 2018-10-11 12:07:15 +0800 | [diff] [blame] | 35 | #define SC27XX_CURVE_SHIFT 8 |
| 36 | #define SC27XX_CURVE_L_MASK GENMASK(7, 0) |
| 37 | #define SC27XX_CURVE_H_MASK GENMASK(15, 8) |
| 38 | |
Baolin Wang | e081c49 | 2018-05-14 14:34:51 +0800 | [diff] [blame] | 39 | #define SC27XX_LEDS_OFFSET 0x10 |
| 40 | #define SC27XX_LEDS_MAX 3 |
Baolin Wang | 8dbac65 | 2018-10-11 12:07:15 +0800 | [diff] [blame] | 41 | #define SC27XX_LEDS_PATTERN_CNT 4 |
| 42 | /* Stage duration step, in milliseconds */ |
| 43 | #define SC27XX_LEDS_STEP 125 |
| 44 | /* Minimum and maximum duration, in milliseconds */ |
| 45 | #define SC27XX_DELTA_T_MIN SC27XX_LEDS_STEP |
| 46 | #define SC27XX_DELTA_T_MAX (SC27XX_LEDS_STEP * 255) |
Baolin Wang | e081c49 | 2018-05-14 14:34:51 +0800 | [diff] [blame] | 47 | |
| 48 | struct sc27xx_led { |
| 49 | char name[LED_MAX_NAME_SIZE]; |
| 50 | struct led_classdev ldev; |
| 51 | struct sc27xx_led_priv *priv; |
| 52 | u8 line; |
| 53 | bool active; |
| 54 | }; |
| 55 | |
| 56 | struct sc27xx_led_priv { |
| 57 | struct sc27xx_led leds[SC27XX_LEDS_MAX]; |
| 58 | struct regmap *regmap; |
| 59 | struct mutex lock; |
| 60 | u32 base; |
| 61 | }; |
| 62 | |
| 63 | #define to_sc27xx_led(ldev) \ |
| 64 | container_of(ldev, struct sc27xx_led, ldev) |
| 65 | |
| 66 | static int sc27xx_led_init(struct regmap *regmap) |
| 67 | { |
| 68 | int err; |
| 69 | |
| 70 | err = regmap_update_bits(regmap, SC27XX_MODULE_EN0, SC27XX_BLTC_EN, |
| 71 | SC27XX_BLTC_EN); |
| 72 | if (err) |
| 73 | return err; |
| 74 | |
| 75 | err = regmap_update_bits(regmap, SC27XX_CLK_EN0, SC27XX_RTC_EN, |
| 76 | SC27XX_RTC_EN); |
| 77 | if (err) |
| 78 | return err; |
| 79 | |
| 80 | return regmap_update_bits(regmap, SC27XX_RGB_CTRL, SC27XX_RGB_PD, 0); |
| 81 | } |
| 82 | |
| 83 | static u32 sc27xx_led_get_offset(struct sc27xx_led *leds) |
| 84 | { |
| 85 | return leds->priv->base + SC27XX_LEDS_OFFSET * leds->line; |
| 86 | } |
| 87 | |
| 88 | static int sc27xx_led_enable(struct sc27xx_led *leds, enum led_brightness value) |
| 89 | { |
| 90 | u32 base = sc27xx_led_get_offset(leds); |
| 91 | u32 ctrl_base = leds->priv->base + SC27XX_LEDS_CTRL; |
| 92 | u8 ctrl_shift = SC27XX_CTRL_SHIFT * leds->line; |
| 93 | struct regmap *regmap = leds->priv->regmap; |
| 94 | int err; |
| 95 | |
| 96 | err = regmap_update_bits(regmap, base + SC27XX_LEDS_DUTY, |
| 97 | SC27XX_DUTY_MASK, |
| 98 | (value << SC27XX_DUTY_SHIFT) | |
| 99 | SC27XX_MOD_MASK); |
| 100 | if (err) |
| 101 | return err; |
| 102 | |
| 103 | return regmap_update_bits(regmap, ctrl_base, |
| 104 | (SC27XX_LED_RUN | SC27XX_LED_TYPE) << ctrl_shift, |
| 105 | (SC27XX_LED_RUN | SC27XX_LED_TYPE) << ctrl_shift); |
| 106 | } |
| 107 | |
| 108 | static int sc27xx_led_disable(struct sc27xx_led *leds) |
| 109 | { |
| 110 | struct regmap *regmap = leds->priv->regmap; |
| 111 | u32 ctrl_base = leds->priv->base + SC27XX_LEDS_CTRL; |
| 112 | u8 ctrl_shift = SC27XX_CTRL_SHIFT * leds->line; |
| 113 | |
| 114 | return regmap_update_bits(regmap, ctrl_base, |
| 115 | (SC27XX_LED_RUN | SC27XX_LED_TYPE) << ctrl_shift, 0); |
| 116 | } |
| 117 | |
| 118 | static int sc27xx_led_set(struct led_classdev *ldev, enum led_brightness value) |
| 119 | { |
| 120 | struct sc27xx_led *leds = to_sc27xx_led(ldev); |
| 121 | int err; |
| 122 | |
| 123 | mutex_lock(&leds->priv->lock); |
| 124 | |
| 125 | if (value == LED_OFF) |
| 126 | err = sc27xx_led_disable(leds); |
| 127 | else |
| 128 | err = sc27xx_led_enable(leds, value); |
| 129 | |
| 130 | mutex_unlock(&leds->priv->lock); |
| 131 | |
| 132 | return err; |
| 133 | } |
| 134 | |
Baolin Wang | 8dbac65 | 2018-10-11 12:07:15 +0800 | [diff] [blame] | 135 | static void sc27xx_led_clamp_align_delta_t(u32 *delta_t) |
| 136 | { |
| 137 | u32 v, offset, t = *delta_t; |
| 138 | |
| 139 | v = t + SC27XX_LEDS_STEP / 2; |
| 140 | v = clamp_t(u32, v, SC27XX_DELTA_T_MIN, SC27XX_DELTA_T_MAX); |
| 141 | offset = v - SC27XX_DELTA_T_MIN; |
| 142 | offset = SC27XX_LEDS_STEP * (offset / SC27XX_LEDS_STEP); |
| 143 | |
| 144 | *delta_t = SC27XX_DELTA_T_MIN + offset; |
| 145 | } |
| 146 | |
| 147 | static int sc27xx_led_pattern_clear(struct led_classdev *ldev) |
| 148 | { |
| 149 | struct sc27xx_led *leds = to_sc27xx_led(ldev); |
| 150 | struct regmap *regmap = leds->priv->regmap; |
| 151 | u32 base = sc27xx_led_get_offset(leds); |
| 152 | u32 ctrl_base = leds->priv->base + SC27XX_LEDS_CTRL; |
| 153 | u8 ctrl_shift = SC27XX_CTRL_SHIFT * leds->line; |
| 154 | int err; |
| 155 | |
| 156 | mutex_lock(&leds->priv->lock); |
| 157 | |
| 158 | /* Reset the rise, high, fall and low time to zero. */ |
| 159 | regmap_write(regmap, base + SC27XX_LEDS_CURVE0, 0); |
| 160 | regmap_write(regmap, base + SC27XX_LEDS_CURVE1, 0); |
| 161 | |
| 162 | err = regmap_update_bits(regmap, ctrl_base, |
| 163 | (SC27XX_LED_RUN | SC27XX_LED_TYPE) << ctrl_shift, 0); |
| 164 | |
| 165 | ldev->brightness = LED_OFF; |
| 166 | |
| 167 | mutex_unlock(&leds->priv->lock); |
| 168 | |
| 169 | return err; |
| 170 | } |
| 171 | |
| 172 | static int sc27xx_led_pattern_set(struct led_classdev *ldev, |
| 173 | struct led_pattern *pattern, |
| 174 | u32 len, int repeat) |
| 175 | { |
| 176 | struct sc27xx_led *leds = to_sc27xx_led(ldev); |
| 177 | u32 base = sc27xx_led_get_offset(leds); |
| 178 | u32 ctrl_base = leds->priv->base + SC27XX_LEDS_CTRL; |
| 179 | u8 ctrl_shift = SC27XX_CTRL_SHIFT * leds->line; |
| 180 | struct regmap *regmap = leds->priv->regmap; |
| 181 | int err; |
| 182 | |
| 183 | /* |
| 184 | * Must contain 4 tuples to configure the rise time, high time, fall |
| 185 | * time and low time to enable the breathing mode. |
| 186 | */ |
| 187 | if (len != SC27XX_LEDS_PATTERN_CNT) |
| 188 | return -EINVAL; |
| 189 | |
| 190 | mutex_lock(&leds->priv->lock); |
| 191 | |
| 192 | sc27xx_led_clamp_align_delta_t(&pattern[0].delta_t); |
| 193 | err = regmap_update_bits(regmap, base + SC27XX_LEDS_CURVE0, |
| 194 | SC27XX_CURVE_L_MASK, |
| 195 | pattern[0].delta_t / SC27XX_LEDS_STEP); |
| 196 | if (err) |
| 197 | goto out; |
| 198 | |
| 199 | sc27xx_led_clamp_align_delta_t(&pattern[1].delta_t); |
| 200 | err = regmap_update_bits(regmap, base + SC27XX_LEDS_CURVE1, |
| 201 | SC27XX_CURVE_L_MASK, |
| 202 | pattern[1].delta_t / SC27XX_LEDS_STEP); |
| 203 | if (err) |
| 204 | goto out; |
| 205 | |
| 206 | sc27xx_led_clamp_align_delta_t(&pattern[2].delta_t); |
| 207 | err = regmap_update_bits(regmap, base + SC27XX_LEDS_CURVE0, |
| 208 | SC27XX_CURVE_H_MASK, |
| 209 | (pattern[2].delta_t / SC27XX_LEDS_STEP) << |
| 210 | SC27XX_CURVE_SHIFT); |
| 211 | if (err) |
| 212 | goto out; |
| 213 | |
| 214 | sc27xx_led_clamp_align_delta_t(&pattern[3].delta_t); |
| 215 | err = regmap_update_bits(regmap, base + SC27XX_LEDS_CURVE1, |
| 216 | SC27XX_CURVE_H_MASK, |
| 217 | (pattern[3].delta_t / SC27XX_LEDS_STEP) << |
| 218 | SC27XX_CURVE_SHIFT); |
| 219 | if (err) |
| 220 | goto out; |
| 221 | |
| 222 | err = regmap_update_bits(regmap, base + SC27XX_LEDS_DUTY, |
| 223 | SC27XX_DUTY_MASK, |
| 224 | (pattern[1].brightness << SC27XX_DUTY_SHIFT) | |
| 225 | SC27XX_MOD_MASK); |
| 226 | if (err) |
| 227 | goto out; |
| 228 | |
| 229 | /* Enable the LED breathing mode */ |
| 230 | err = regmap_update_bits(regmap, ctrl_base, |
| 231 | SC27XX_LED_RUN << ctrl_shift, |
| 232 | SC27XX_LED_RUN << ctrl_shift); |
| 233 | if (!err) |
| 234 | ldev->brightness = pattern[1].brightness; |
| 235 | |
| 236 | out: |
| 237 | mutex_unlock(&leds->priv->lock); |
| 238 | |
| 239 | return err; |
| 240 | } |
| 241 | |
Baolin Wang | e081c49 | 2018-05-14 14:34:51 +0800 | [diff] [blame] | 242 | static int sc27xx_led_register(struct device *dev, struct sc27xx_led_priv *priv) |
| 243 | { |
| 244 | int i, err; |
| 245 | |
| 246 | err = sc27xx_led_init(priv->regmap); |
| 247 | if (err) |
| 248 | return err; |
| 249 | |
| 250 | for (i = 0; i < SC27XX_LEDS_MAX; i++) { |
| 251 | struct sc27xx_led *led = &priv->leds[i]; |
| 252 | |
| 253 | if (!led->active) |
| 254 | continue; |
| 255 | |
| 256 | led->line = i; |
| 257 | led->priv = priv; |
| 258 | led->ldev.name = led->name; |
| 259 | led->ldev.brightness_set_blocking = sc27xx_led_set; |
Baolin Wang | 8dbac65 | 2018-10-11 12:07:15 +0800 | [diff] [blame] | 260 | led->ldev.pattern_set = sc27xx_led_pattern_set; |
| 261 | led->ldev.pattern_clear = sc27xx_led_pattern_clear; |
| 262 | led->ldev.default_trigger = "pattern"; |
Baolin Wang | e081c49 | 2018-05-14 14:34:51 +0800 | [diff] [blame] | 263 | |
| 264 | err = devm_led_classdev_register(dev, &led->ldev); |
| 265 | if (err) |
| 266 | return err; |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static int sc27xx_led_probe(struct platform_device *pdev) |
| 273 | { |
| 274 | struct device *dev = &pdev->dev; |
| 275 | struct device_node *np = dev->of_node, *child; |
| 276 | struct sc27xx_led_priv *priv; |
| 277 | const char *str; |
| 278 | u32 base, count, reg; |
| 279 | int err; |
| 280 | |
| 281 | count = of_get_child_count(np); |
| 282 | if (!count || count > SC27XX_LEDS_MAX) |
| 283 | return -EINVAL; |
| 284 | |
| 285 | err = of_property_read_u32(np, "reg", &base); |
| 286 | if (err) { |
| 287 | dev_err(dev, "fail to get reg of property\n"); |
| 288 | return err; |
| 289 | } |
| 290 | |
| 291 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 292 | if (!priv) |
| 293 | return -ENOMEM; |
| 294 | |
| 295 | platform_set_drvdata(pdev, priv); |
| 296 | mutex_init(&priv->lock); |
| 297 | priv->base = base; |
| 298 | priv->regmap = dev_get_regmap(dev->parent, NULL); |
Wei Yongjun | 43926c2 | 2018-05-22 11:45:56 +0000 | [diff] [blame] | 299 | if (!priv->regmap) { |
| 300 | err = -ENODEV; |
Baolin Wang | e081c49 | 2018-05-14 14:34:51 +0800 | [diff] [blame] | 301 | dev_err(dev, "failed to get regmap: %d\n", err); |
| 302 | return err; |
| 303 | } |
| 304 | |
| 305 | for_each_child_of_node(np, child) { |
| 306 | err = of_property_read_u32(child, "reg", ®); |
| 307 | if (err) { |
| 308 | of_node_put(child); |
| 309 | mutex_destroy(&priv->lock); |
| 310 | return err; |
| 311 | } |
| 312 | |
| 313 | if (reg >= SC27XX_LEDS_MAX || priv->leds[reg].active) { |
| 314 | of_node_put(child); |
| 315 | mutex_destroy(&priv->lock); |
| 316 | return -EINVAL; |
| 317 | } |
| 318 | |
| 319 | priv->leds[reg].active = true; |
| 320 | |
| 321 | err = of_property_read_string(child, "label", &str); |
| 322 | if (err) |
| 323 | snprintf(priv->leds[reg].name, LED_MAX_NAME_SIZE, |
| 324 | "sc27xx::"); |
| 325 | else |
| 326 | snprintf(priv->leds[reg].name, LED_MAX_NAME_SIZE, |
| 327 | "sc27xx:%s", str); |
| 328 | } |
| 329 | |
| 330 | err = sc27xx_led_register(dev, priv); |
| 331 | if (err) |
| 332 | mutex_destroy(&priv->lock); |
| 333 | |
| 334 | return err; |
| 335 | } |
| 336 | |
| 337 | static int sc27xx_led_remove(struct platform_device *pdev) |
| 338 | { |
| 339 | struct sc27xx_led_priv *priv = platform_get_drvdata(pdev); |
| 340 | |
| 341 | mutex_destroy(&priv->lock); |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static const struct of_device_id sc27xx_led_of_match[] = { |
| 346 | { .compatible = "sprd,sc2731-bltc", }, |
| 347 | { } |
| 348 | }; |
| 349 | MODULE_DEVICE_TABLE(of, sc27xx_led_of_match); |
| 350 | |
| 351 | static struct platform_driver sc27xx_led_driver = { |
| 352 | .driver = { |
| 353 | .name = "sprd-bltc", |
| 354 | .of_match_table = sc27xx_led_of_match, |
| 355 | }, |
| 356 | .probe = sc27xx_led_probe, |
| 357 | .remove = sc27xx_led_remove, |
| 358 | }; |
| 359 | |
| 360 | module_platform_driver(sc27xx_led_driver); |
| 361 | |
| 362 | MODULE_DESCRIPTION("Spreadtrum SC27xx breathing light controller driver"); |
| 363 | MODULE_AUTHOR("Xiaotong Lu <xiaotong.lu@spreadtrum.com>"); |
Baolin Wang | 8dbac65 | 2018-10-11 12:07:15 +0800 | [diff] [blame] | 364 | MODULE_AUTHOR("Baolin Wang <baolin.wang@linaro.org>"); |
Baolin Wang | e081c49 | 2018-05-14 14:34:51 +0800 | [diff] [blame] | 365 | MODULE_LICENSE("GPL v2"); |