Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 1 | /* |
Kim, Milo | ff45262a | 2013-02-18 21:10:14 -0800 | [diff] [blame] | 2 | * LP5521/LP5523/LP55231/LP5562 Common Driver |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2012 Texas Instruments |
| 5 | * |
| 6 | * Author: Milo(Woogyom) Kim <milo.kim@ti.com> |
| 7 | * |
| 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. |
| 11 | * |
| 12 | * Derived from leds-lp5521.c, leds-lp5523.c |
| 13 | */ |
| 14 | |
Kim, Milo | 53b4192 | 2013-03-20 17:37:00 -0700 | [diff] [blame] | 15 | #include <linux/clk.h> |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 16 | #include <linux/delay.h> |
Milo(Woogyom) Kim | 10c06d1 | 2013-02-05 19:17:20 +0900 | [diff] [blame] | 17 | #include <linux/firmware.h> |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 18 | #include <linux/i2c.h> |
| 19 | #include <linux/leds.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/platform_data/leds-lp55xx.h> |
Linus Walleij | 7542a04b | 2013-04-23 04:52:59 -0700 | [diff] [blame] | 22 | #include <linux/slab.h> |
Sebastian Reichel | 30dae2f | 2013-10-22 11:02:56 -0700 | [diff] [blame] | 23 | #include <linux/gpio.h> |
| 24 | #include <linux/of_gpio.h> |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 25 | |
| 26 | #include "leds-lp55xx-common.h" |
| 27 | |
Kim, Milo | 53b4192 | 2013-03-20 17:37:00 -0700 | [diff] [blame] | 28 | /* External clock rate */ |
| 29 | #define LP55XX_CLK_32K 32768 |
| 30 | |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame] | 31 | static struct lp55xx_led *cdev_to_lp55xx_led(struct led_classdev *cdev) |
| 32 | { |
| 33 | return container_of(cdev, struct lp55xx_led, cdev); |
| 34 | } |
| 35 | |
Milo(Woogyom) Kim | a96bfa1 | 2013-02-05 19:09:32 +0900 | [diff] [blame] | 36 | static struct lp55xx_led *dev_to_lp55xx_led(struct device *dev) |
| 37 | { |
| 38 | return cdev_to_lp55xx_led(dev_get_drvdata(dev)); |
| 39 | } |
| 40 | |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 41 | static void lp55xx_reset_device(struct lp55xx_chip *chip) |
| 42 | { |
| 43 | struct lp55xx_device_config *cfg = chip->cfg; |
| 44 | u8 addr = cfg->reset.addr; |
| 45 | u8 val = cfg->reset.val; |
| 46 | |
| 47 | /* no error checking here because no ACK from the device after reset */ |
| 48 | lp55xx_write(chip, addr, val); |
| 49 | } |
| 50 | |
Milo(Woogyom) Kim | e3a700d | 2013-02-05 18:09:56 +0900 | [diff] [blame] | 51 | static int lp55xx_detect_device(struct lp55xx_chip *chip) |
| 52 | { |
| 53 | struct lp55xx_device_config *cfg = chip->cfg; |
| 54 | u8 addr = cfg->enable.addr; |
| 55 | u8 val = cfg->enable.val; |
| 56 | int ret; |
| 57 | |
| 58 | ret = lp55xx_write(chip, addr, val); |
| 59 | if (ret) |
| 60 | return ret; |
| 61 | |
| 62 | usleep_range(1000, 2000); |
| 63 | |
| 64 | ret = lp55xx_read(chip, addr, &val); |
| 65 | if (ret) |
| 66 | return ret; |
| 67 | |
| 68 | if (val != cfg->enable.val) |
| 69 | return -ENODEV; |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 74 | static int lp55xx_post_init_device(struct lp55xx_chip *chip) |
| 75 | { |
| 76 | struct lp55xx_device_config *cfg = chip->cfg; |
| 77 | |
| 78 | if (!cfg->post_init_device) |
| 79 | return 0; |
| 80 | |
| 81 | return cfg->post_init_device(chip); |
| 82 | } |
| 83 | |
Milo(Woogyom) Kim | a96bfa1 | 2013-02-05 19:09:32 +0900 | [diff] [blame] | 84 | static ssize_t lp55xx_show_current(struct device *dev, |
| 85 | struct device_attribute *attr, |
| 86 | char *buf) |
| 87 | { |
| 88 | struct lp55xx_led *led = dev_to_lp55xx_led(dev); |
| 89 | |
Kim, Milo | 24d3212 | 2013-03-14 17:19:36 -0700 | [diff] [blame] | 90 | return scnprintf(buf, PAGE_SIZE, "%d\n", led->led_current); |
Milo(Woogyom) Kim | a96bfa1 | 2013-02-05 19:09:32 +0900 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static ssize_t lp55xx_store_current(struct device *dev, |
| 94 | struct device_attribute *attr, |
| 95 | const char *buf, size_t len) |
| 96 | { |
| 97 | struct lp55xx_led *led = dev_to_lp55xx_led(dev); |
| 98 | struct lp55xx_chip *chip = led->chip; |
| 99 | unsigned long curr; |
| 100 | |
| 101 | if (kstrtoul(buf, 0, &curr)) |
| 102 | return -EINVAL; |
| 103 | |
| 104 | if (curr > led->max_current) |
| 105 | return -EINVAL; |
| 106 | |
| 107 | if (!chip->cfg->set_led_current) |
| 108 | return len; |
| 109 | |
| 110 | mutex_lock(&chip->lock); |
| 111 | chip->cfg->set_led_current(led, (u8)curr); |
| 112 | mutex_unlock(&chip->lock); |
| 113 | |
| 114 | return len; |
| 115 | } |
| 116 | |
| 117 | static ssize_t lp55xx_show_max_current(struct device *dev, |
| 118 | struct device_attribute *attr, |
| 119 | char *buf) |
| 120 | { |
| 121 | struct lp55xx_led *led = dev_to_lp55xx_led(dev); |
| 122 | |
Kim, Milo | 24d3212 | 2013-03-14 17:19:36 -0700 | [diff] [blame] | 123 | return scnprintf(buf, PAGE_SIZE, "%d\n", led->max_current); |
Milo(Woogyom) Kim | a96bfa1 | 2013-02-05 19:09:32 +0900 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static DEVICE_ATTR(led_current, S_IRUGO | S_IWUSR, lp55xx_show_current, |
| 127 | lp55xx_store_current); |
| 128 | static DEVICE_ATTR(max_current, S_IRUGO , lp55xx_show_max_current, NULL); |
| 129 | |
Johan Hovold | 44a1255 | 2014-06-25 10:08:56 -0700 | [diff] [blame] | 130 | static struct attribute *lp55xx_led_attrs[] = { |
Milo(Woogyom) Kim | a96bfa1 | 2013-02-05 19:09:32 +0900 | [diff] [blame] | 131 | &dev_attr_led_current.attr, |
| 132 | &dev_attr_max_current.attr, |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 133 | NULL, |
| 134 | }; |
Johan Hovold | 44a1255 | 2014-06-25 10:08:56 -0700 | [diff] [blame] | 135 | ATTRIBUTE_GROUPS(lp55xx_led); |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 136 | |
| 137 | static void lp55xx_set_brightness(struct led_classdev *cdev, |
| 138 | enum led_brightness brightness) |
| 139 | { |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame] | 140 | struct lp55xx_led *led = cdev_to_lp55xx_led(cdev); |
| 141 | |
| 142 | led->brightness = (u8)brightness; |
| 143 | schedule_work(&led->brightness_work); |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 144 | } |
| 145 | |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 146 | static int lp55xx_init_led(struct lp55xx_led *led, |
| 147 | struct lp55xx_chip *chip, int chan) |
| 148 | { |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 149 | struct lp55xx_platform_data *pdata = chip->pdata; |
| 150 | struct lp55xx_device_config *cfg = chip->cfg; |
| 151 | struct device *dev = &chip->cl->dev; |
| 152 | char name[32]; |
| 153 | int ret; |
| 154 | int max_channel = cfg->max_channel; |
| 155 | |
| 156 | if (chan >= max_channel) { |
| 157 | dev_err(dev, "invalid channel: %d / %d\n", chan, max_channel); |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | |
| 161 | if (pdata->led_config[chan].led_current == 0) |
| 162 | return 0; |
| 163 | |
| 164 | led->led_current = pdata->led_config[chan].led_current; |
| 165 | led->max_current = pdata->led_config[chan].max_current; |
| 166 | led->chan_nr = pdata->led_config[chan].chan_nr; |
Linus Walleij | f65f0a1 | 2013-09-15 03:50:17 -0700 | [diff] [blame] | 167 | led->cdev.default_trigger = pdata->led_config[chan].default_trigger; |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 168 | |
| 169 | if (led->chan_nr >= max_channel) { |
| 170 | dev_err(dev, "Use channel numbers between 0 and %d\n", |
| 171 | max_channel - 1); |
| 172 | return -EINVAL; |
| 173 | } |
| 174 | |
| 175 | led->cdev.brightness_set = lp55xx_set_brightness; |
Johan Hovold | 44a1255 | 2014-06-25 10:08:56 -0700 | [diff] [blame] | 176 | led->cdev.groups = lp55xx_led_groups; |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 177 | |
| 178 | if (pdata->led_config[chan].name) { |
| 179 | led->cdev.name = pdata->led_config[chan].name; |
| 180 | } else { |
| 181 | snprintf(name, sizeof(name), "%s:channel%d", |
| 182 | pdata->label ? : chip->cl->name, chan); |
| 183 | led->cdev.name = name; |
| 184 | } |
| 185 | |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 186 | ret = led_classdev_register(dev, &led->cdev); |
| 187 | if (ret) { |
| 188 | dev_err(dev, "led register err: %d\n", ret); |
| 189 | return ret; |
| 190 | } |
| 191 | |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 192 | return 0; |
| 193 | } |
| 194 | |
Milo(Woogyom) Kim | 10c06d1 | 2013-02-05 19:17:20 +0900 | [diff] [blame] | 195 | static void lp55xx_firmware_loaded(const struct firmware *fw, void *context) |
| 196 | { |
| 197 | struct lp55xx_chip *chip = context; |
| 198 | struct device *dev = &chip->cl->dev; |
Milo Kim | 93ad8a1 | 2013-11-20 22:14:34 -0800 | [diff] [blame] | 199 | enum lp55xx_engine_index idx = chip->engine_idx; |
Milo(Woogyom) Kim | 10c06d1 | 2013-02-05 19:17:20 +0900 | [diff] [blame] | 200 | |
| 201 | if (!fw) { |
| 202 | dev_err(dev, "firmware request failed\n"); |
| 203 | goto out; |
| 204 | } |
| 205 | |
| 206 | /* handling firmware data is chip dependent */ |
| 207 | mutex_lock(&chip->lock); |
| 208 | |
Milo Kim | 93ad8a1 | 2013-11-20 22:14:34 -0800 | [diff] [blame] | 209 | chip->engines[idx - 1].mode = LP55XX_ENGINE_LOAD; |
Milo(Woogyom) Kim | 10c06d1 | 2013-02-05 19:17:20 +0900 | [diff] [blame] | 210 | chip->fw = fw; |
| 211 | if (chip->cfg->firmware_cb) |
| 212 | chip->cfg->firmware_cb(chip); |
| 213 | |
| 214 | mutex_unlock(&chip->lock); |
| 215 | |
| 216 | out: |
| 217 | /* firmware should be released for other channel use */ |
| 218 | release_firmware(chip->fw); |
| 219 | } |
| 220 | |
| 221 | static int lp55xx_request_firmware(struct lp55xx_chip *chip) |
| 222 | { |
| 223 | const char *name = chip->cl->name; |
| 224 | struct device *dev = &chip->cl->dev; |
| 225 | |
Milo Kim | b678932 | 2015-06-28 17:39:14 -0700 | [diff] [blame] | 226 | return request_firmware_nowait(THIS_MODULE, false, name, dev, |
Milo(Woogyom) Kim | 10c06d1 | 2013-02-05 19:17:20 +0900 | [diff] [blame] | 227 | GFP_KERNEL, chip, lp55xx_firmware_loaded); |
| 228 | } |
| 229 | |
| 230 | static ssize_t lp55xx_show_engine_select(struct device *dev, |
| 231 | struct device_attribute *attr, |
| 232 | char *buf) |
| 233 | { |
| 234 | struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev)); |
| 235 | struct lp55xx_chip *chip = led->chip; |
| 236 | |
| 237 | return sprintf(buf, "%d\n", chip->engine_idx); |
| 238 | } |
| 239 | |
| 240 | static ssize_t lp55xx_store_engine_select(struct device *dev, |
| 241 | struct device_attribute *attr, |
| 242 | const char *buf, size_t len) |
| 243 | { |
| 244 | struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev)); |
| 245 | struct lp55xx_chip *chip = led->chip; |
| 246 | unsigned long val; |
| 247 | int ret; |
| 248 | |
| 249 | if (kstrtoul(buf, 0, &val)) |
| 250 | return -EINVAL; |
| 251 | |
| 252 | /* select the engine to be run */ |
| 253 | |
| 254 | switch (val) { |
| 255 | case LP55XX_ENGINE_1: |
| 256 | case LP55XX_ENGINE_2: |
| 257 | case LP55XX_ENGINE_3: |
| 258 | mutex_lock(&chip->lock); |
| 259 | chip->engine_idx = val; |
| 260 | ret = lp55xx_request_firmware(chip); |
| 261 | mutex_unlock(&chip->lock); |
| 262 | break; |
| 263 | default: |
| 264 | dev_err(dev, "%lu: invalid engine index. (1, 2, 3)\n", val); |
| 265 | return -EINVAL; |
| 266 | } |
| 267 | |
| 268 | if (ret) { |
| 269 | dev_err(dev, "request firmware err: %d\n", ret); |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | return len; |
| 274 | } |
| 275 | |
| 276 | static inline void lp55xx_run_engine(struct lp55xx_chip *chip, bool start) |
| 277 | { |
| 278 | if (chip->cfg->run_engine) |
| 279 | chip->cfg->run_engine(chip, start); |
| 280 | } |
| 281 | |
| 282 | static ssize_t lp55xx_store_engine_run(struct device *dev, |
| 283 | struct device_attribute *attr, |
| 284 | const char *buf, size_t len) |
| 285 | { |
| 286 | struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev)); |
| 287 | struct lp55xx_chip *chip = led->chip; |
| 288 | unsigned long val; |
| 289 | |
| 290 | if (kstrtoul(buf, 0, &val)) |
| 291 | return -EINVAL; |
| 292 | |
| 293 | /* run or stop the selected engine */ |
| 294 | |
| 295 | if (val <= 0) { |
| 296 | lp55xx_run_engine(chip, false); |
| 297 | return len; |
| 298 | } |
| 299 | |
| 300 | mutex_lock(&chip->lock); |
| 301 | lp55xx_run_engine(chip, true); |
| 302 | mutex_unlock(&chip->lock); |
| 303 | |
| 304 | return len; |
| 305 | } |
| 306 | |
| 307 | static DEVICE_ATTR(select_engine, S_IRUGO | S_IWUSR, |
| 308 | lp55xx_show_engine_select, lp55xx_store_engine_select); |
| 309 | static DEVICE_ATTR(run_engine, S_IWUSR, NULL, lp55xx_store_engine_run); |
| 310 | |
Milo(Woogyom) Kim | b3b6f81 | 2013-02-05 19:15:27 +0900 | [diff] [blame] | 311 | static struct attribute *lp55xx_engine_attributes[] = { |
Milo(Woogyom) Kim | 10c06d1 | 2013-02-05 19:17:20 +0900 | [diff] [blame] | 312 | &dev_attr_select_engine.attr, |
| 313 | &dev_attr_run_engine.attr, |
Milo(Woogyom) Kim | b3b6f81 | 2013-02-05 19:15:27 +0900 | [diff] [blame] | 314 | NULL, |
| 315 | }; |
| 316 | |
| 317 | static const struct attribute_group lp55xx_engine_attr_group = { |
| 318 | .attrs = lp55xx_engine_attributes, |
| 319 | }; |
| 320 | |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 321 | int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val) |
| 322 | { |
| 323 | return i2c_smbus_write_byte_data(chip->cl, reg, val); |
| 324 | } |
| 325 | EXPORT_SYMBOL_GPL(lp55xx_write); |
| 326 | |
| 327 | int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val) |
| 328 | { |
| 329 | s32 ret; |
| 330 | |
| 331 | ret = i2c_smbus_read_byte_data(chip->cl, reg); |
| 332 | if (ret < 0) |
| 333 | return ret; |
| 334 | |
| 335 | *val = ret; |
| 336 | return 0; |
| 337 | } |
| 338 | EXPORT_SYMBOL_GPL(lp55xx_read); |
| 339 | |
| 340 | int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val) |
| 341 | { |
| 342 | int ret; |
| 343 | u8 tmp; |
| 344 | |
| 345 | ret = lp55xx_read(chip, reg, &tmp); |
| 346 | if (ret) |
| 347 | return ret; |
| 348 | |
| 349 | tmp &= ~mask; |
| 350 | tmp |= val & mask; |
| 351 | |
| 352 | return lp55xx_write(chip, reg, tmp); |
| 353 | } |
| 354 | EXPORT_SYMBOL_GPL(lp55xx_update_bits); |
| 355 | |
Kim, Milo | 53b4192 | 2013-03-20 17:37:00 -0700 | [diff] [blame] | 356 | bool lp55xx_is_extclk_used(struct lp55xx_chip *chip) |
| 357 | { |
| 358 | struct clk *clk; |
| 359 | int err; |
| 360 | |
| 361 | clk = devm_clk_get(&chip->cl->dev, "32k_clk"); |
| 362 | if (IS_ERR(clk)) |
| 363 | goto use_internal_clk; |
| 364 | |
| 365 | err = clk_prepare_enable(clk); |
| 366 | if (err) |
| 367 | goto use_internal_clk; |
| 368 | |
| 369 | if (clk_get_rate(clk) != LP55XX_CLK_32K) { |
| 370 | clk_disable_unprepare(clk); |
| 371 | goto use_internal_clk; |
| 372 | } |
| 373 | |
| 374 | dev_info(&chip->cl->dev, "%dHz external clock used\n", LP55XX_CLK_32K); |
| 375 | |
| 376 | chip->clk = clk; |
| 377 | return true; |
| 378 | |
| 379 | use_internal_clk: |
| 380 | dev_info(&chip->cl->dev, "internal clock used\n"); |
| 381 | return false; |
| 382 | } |
| 383 | EXPORT_SYMBOL_GPL(lp55xx_is_extclk_used); |
| 384 | |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 385 | int lp55xx_init_device(struct lp55xx_chip *chip) |
| 386 | { |
| 387 | struct lp55xx_platform_data *pdata; |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 388 | struct lp55xx_device_config *cfg; |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 389 | struct device *dev = &chip->cl->dev; |
| 390 | int ret = 0; |
| 391 | |
| 392 | WARN_ON(!chip); |
| 393 | |
| 394 | pdata = chip->pdata; |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 395 | cfg = chip->cfg; |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 396 | |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 397 | if (!pdata || !cfg) |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 398 | return -EINVAL; |
| 399 | |
Sebastian Reichel | 30dae2f | 2013-10-22 11:02:56 -0700 | [diff] [blame] | 400 | if (gpio_is_valid(pdata->enable_gpio)) { |
| 401 | ret = devm_gpio_request_one(dev, pdata->enable_gpio, |
| 402 | GPIOF_DIR_OUT, "lp5523_enable"); |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 403 | if (ret < 0) { |
Sebastian Reichel | 30dae2f | 2013-10-22 11:02:56 -0700 | [diff] [blame] | 404 | dev_err(dev, "could not acquire enable gpio (err=%d)\n", |
| 405 | ret); |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 406 | goto err; |
| 407 | } |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 408 | |
Sebastian Reichel | 30dae2f | 2013-10-22 11:02:56 -0700 | [diff] [blame] | 409 | gpio_set_value(pdata->enable_gpio, 0); |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 410 | usleep_range(1000, 2000); /* Keep enable down at least 1ms */ |
Sebastian Reichel | 30dae2f | 2013-10-22 11:02:56 -0700 | [diff] [blame] | 411 | gpio_set_value(pdata->enable_gpio, 1); |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 412 | usleep_range(1000, 2000); /* 500us abs min. */ |
| 413 | } |
| 414 | |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 415 | lp55xx_reset_device(chip); |
| 416 | |
| 417 | /* |
| 418 | * Exact value is not available. 10 - 20ms |
| 419 | * appears to be enough for reset. |
| 420 | */ |
| 421 | usleep_range(10000, 20000); |
| 422 | |
Milo(Woogyom) Kim | e3a700d | 2013-02-05 18:09:56 +0900 | [diff] [blame] | 423 | ret = lp55xx_detect_device(chip); |
| 424 | if (ret) { |
| 425 | dev_err(dev, "device detection err: %d\n", ret); |
| 426 | goto err; |
| 427 | } |
| 428 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 429 | /* chip specific initialization */ |
| 430 | ret = lp55xx_post_init_device(chip); |
Milo(Woogyom) Kim | 22ebeb4 | 2013-02-05 18:58:35 +0900 | [diff] [blame] | 431 | if (ret) { |
| 432 | dev_err(dev, "post init device err: %d\n", ret); |
| 433 | goto err_post_init; |
| 434 | } |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 435 | |
| 436 | return 0; |
| 437 | |
Milo(Woogyom) Kim | 22ebeb4 | 2013-02-05 18:58:35 +0900 | [diff] [blame] | 438 | err_post_init: |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 439 | lp55xx_deinit_device(chip); |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 440 | err: |
| 441 | return ret; |
| 442 | } |
| 443 | EXPORT_SYMBOL_GPL(lp55xx_init_device); |
| 444 | |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 445 | void lp55xx_deinit_device(struct lp55xx_chip *chip) |
| 446 | { |
| 447 | struct lp55xx_platform_data *pdata = chip->pdata; |
| 448 | |
Kim, Milo | 53b4192 | 2013-03-20 17:37:00 -0700 | [diff] [blame] | 449 | if (chip->clk) |
| 450 | clk_disable_unprepare(chip->clk); |
| 451 | |
Sebastian Reichel | 30dae2f | 2013-10-22 11:02:56 -0700 | [diff] [blame] | 452 | if (gpio_is_valid(pdata->enable_gpio)) |
| 453 | gpio_set_value(pdata->enable_gpio, 0); |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 454 | } |
| 455 | EXPORT_SYMBOL_GPL(lp55xx_deinit_device); |
| 456 | |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 457 | int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip) |
| 458 | { |
| 459 | struct lp55xx_platform_data *pdata = chip->pdata; |
| 460 | struct lp55xx_device_config *cfg = chip->cfg; |
| 461 | int num_channels = pdata->num_channels; |
| 462 | struct lp55xx_led *each; |
| 463 | u8 led_current; |
| 464 | int ret; |
| 465 | int i; |
| 466 | |
| 467 | if (!cfg->brightness_work_fn) { |
| 468 | dev_err(&chip->cl->dev, "empty brightness configuration\n"); |
| 469 | return -EINVAL; |
| 470 | } |
| 471 | |
| 472 | for (i = 0; i < num_channels; i++) { |
| 473 | |
| 474 | /* do not initialize channels that are not connected */ |
| 475 | if (pdata->led_config[i].led_current == 0) |
| 476 | continue; |
| 477 | |
| 478 | led_current = pdata->led_config[i].led_current; |
| 479 | each = led + i; |
| 480 | ret = lp55xx_init_led(each, chip, i); |
| 481 | if (ret) |
| 482 | goto err_init_led; |
| 483 | |
| 484 | INIT_WORK(&each->brightness_work, cfg->brightness_work_fn); |
| 485 | |
| 486 | chip->num_leds++; |
| 487 | each->chip = chip; |
| 488 | |
| 489 | /* setting led current at each channel */ |
| 490 | if (cfg->set_led_current) |
| 491 | cfg->set_led_current(each, led_current); |
| 492 | } |
| 493 | |
| 494 | return 0; |
| 495 | |
| 496 | err_init_led: |
Milo(Woogyom) Kim | d9067d2 | 2013-02-05 19:12:47 +0900 | [diff] [blame] | 497 | lp55xx_unregister_leds(led, chip); |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 498 | return ret; |
| 499 | } |
| 500 | EXPORT_SYMBOL_GPL(lp55xx_register_leds); |
| 501 | |
Milo(Woogyom) Kim | c3a68eb | 2013-02-05 19:11:18 +0900 | [diff] [blame] | 502 | void lp55xx_unregister_leds(struct lp55xx_led *led, struct lp55xx_chip *chip) |
| 503 | { |
| 504 | int i; |
| 505 | struct lp55xx_led *each; |
Milo(Woogyom) Kim | c3a68eb | 2013-02-05 19:11:18 +0900 | [diff] [blame] | 506 | |
| 507 | for (i = 0; i < chip->num_leds; i++) { |
| 508 | each = led + i; |
Milo(Woogyom) Kim | c3a68eb | 2013-02-05 19:11:18 +0900 | [diff] [blame] | 509 | led_classdev_unregister(&each->cdev); |
| 510 | flush_work(&each->brightness_work); |
| 511 | } |
| 512 | } |
| 513 | EXPORT_SYMBOL_GPL(lp55xx_unregister_leds); |
| 514 | |
Milo(Woogyom) Kim | b3b6f81 | 2013-02-05 19:15:27 +0900 | [diff] [blame] | 515 | int lp55xx_register_sysfs(struct lp55xx_chip *chip) |
| 516 | { |
| 517 | struct device *dev = &chip->cl->dev; |
Milo(Woogyom) Kim | 240085e | 2013-02-05 19:20:01 +0900 | [diff] [blame] | 518 | struct lp55xx_device_config *cfg = chip->cfg; |
| 519 | int ret; |
Milo(Woogyom) Kim | b3b6f81 | 2013-02-05 19:15:27 +0900 | [diff] [blame] | 520 | |
Milo(Woogyom) Kim | 240085e | 2013-02-05 19:20:01 +0900 | [diff] [blame] | 521 | if (!cfg->run_engine || !cfg->firmware_cb) |
| 522 | goto dev_specific_attrs; |
| 523 | |
| 524 | ret = sysfs_create_group(&dev->kobj, &lp55xx_engine_attr_group); |
| 525 | if (ret) |
| 526 | return ret; |
| 527 | |
| 528 | dev_specific_attrs: |
| 529 | return cfg->dev_attr_group ? |
| 530 | sysfs_create_group(&dev->kobj, cfg->dev_attr_group) : 0; |
Milo(Woogyom) Kim | b3b6f81 | 2013-02-05 19:15:27 +0900 | [diff] [blame] | 531 | } |
| 532 | EXPORT_SYMBOL_GPL(lp55xx_register_sysfs); |
| 533 | |
Milo(Woogyom) Kim | ba6fa84 | 2013-02-05 19:23:04 +0900 | [diff] [blame] | 534 | void lp55xx_unregister_sysfs(struct lp55xx_chip *chip) |
| 535 | { |
| 536 | struct device *dev = &chip->cl->dev; |
| 537 | struct lp55xx_device_config *cfg = chip->cfg; |
| 538 | |
| 539 | if (cfg->dev_attr_group) |
| 540 | sysfs_remove_group(&dev->kobj, cfg->dev_attr_group); |
| 541 | |
| 542 | sysfs_remove_group(&dev->kobj, &lp55xx_engine_attr_group); |
| 543 | } |
| 544 | EXPORT_SYMBOL_GPL(lp55xx_unregister_sysfs); |
| 545 | |
Milo Kim | ed133352 | 2015-08-24 16:09:55 +0900 | [diff] [blame^] | 546 | struct lp55xx_platform_data *lp55xx_of_populate_pdata(struct device *dev, |
| 547 | struct device_node *np) |
Linus Walleij | 7542a04b | 2013-04-23 04:52:59 -0700 | [diff] [blame] | 548 | { |
Kim, Milo | 2dac912 | 2013-05-07 00:14:48 -0700 | [diff] [blame] | 549 | struct device_node *child; |
Linus Walleij | 7542a04b | 2013-04-23 04:52:59 -0700 | [diff] [blame] | 550 | struct lp55xx_platform_data *pdata; |
Kim, Milo | 2dac912 | 2013-05-07 00:14:48 -0700 | [diff] [blame] | 551 | struct lp55xx_led_config *cfg; |
| 552 | int num_channels; |
| 553 | int i = 0; |
Linus Walleij | 7542a04b | 2013-04-23 04:52:59 -0700 | [diff] [blame] | 554 | |
| 555 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 556 | if (!pdata) |
Milo Kim | ed133352 | 2015-08-24 16:09:55 +0900 | [diff] [blame^] | 557 | return ERR_PTR(-ENOMEM); |
Linus Walleij | 7542a04b | 2013-04-23 04:52:59 -0700 | [diff] [blame] | 558 | |
Kim, Milo | 2dac912 | 2013-05-07 00:14:48 -0700 | [diff] [blame] | 559 | num_channels = of_get_child_count(np); |
| 560 | if (num_channels == 0) { |
| 561 | dev_err(dev, "no LED channels\n"); |
Milo Kim | ed133352 | 2015-08-24 16:09:55 +0900 | [diff] [blame^] | 562 | return ERR_PTR(-EINVAL); |
Kim, Milo | 2dac912 | 2013-05-07 00:14:48 -0700 | [diff] [blame] | 563 | } |
Linus Walleij | 7542a04b | 2013-04-23 04:52:59 -0700 | [diff] [blame] | 564 | |
Kim, Milo | 2dac912 | 2013-05-07 00:14:48 -0700 | [diff] [blame] | 565 | cfg = devm_kzalloc(dev, sizeof(*cfg) * num_channels, GFP_KERNEL); |
| 566 | if (!cfg) |
Milo Kim | ed133352 | 2015-08-24 16:09:55 +0900 | [diff] [blame^] | 567 | return ERR_PTR(-ENOMEM); |
Linus Walleij | 7542a04b | 2013-04-23 04:52:59 -0700 | [diff] [blame] | 568 | |
Kim, Milo | 2dac912 | 2013-05-07 00:14:48 -0700 | [diff] [blame] | 569 | pdata->led_config = &cfg[0]; |
| 570 | pdata->num_channels = num_channels; |
| 571 | |
| 572 | for_each_child_of_node(np, child) { |
| 573 | cfg[i].chan_nr = i; |
| 574 | |
| 575 | of_property_read_string(child, "chan-name", &cfg[i].name); |
| 576 | of_property_read_u8(child, "led-cur", &cfg[i].led_current); |
| 577 | of_property_read_u8(child, "max-cur", &cfg[i].max_current); |
Linus Walleij | f65f0a1 | 2013-09-15 03:50:17 -0700 | [diff] [blame] | 578 | cfg[i].default_trigger = |
| 579 | of_get_property(child, "linux,default-trigger", NULL); |
Kim, Milo | 2dac912 | 2013-05-07 00:14:48 -0700 | [diff] [blame] | 580 | |
| 581 | i++; |
Linus Walleij | 7542a04b | 2013-04-23 04:52:59 -0700 | [diff] [blame] | 582 | } |
Kim, Milo | 2dac912 | 2013-05-07 00:14:48 -0700 | [diff] [blame] | 583 | |
| 584 | of_property_read_string(np, "label", &pdata->label); |
| 585 | of_property_read_u8(np, "clock-mode", &pdata->clock_mode); |
| 586 | |
Sebastian Reichel | 30dae2f | 2013-10-22 11:02:56 -0700 | [diff] [blame] | 587 | pdata->enable_gpio = of_get_named_gpio(np, "enable-gpio", 0); |
| 588 | |
Kim, Milo | 33b3a56 | 2013-07-09 02:11:37 -0700 | [diff] [blame] | 589 | /* LP8501 specific */ |
| 590 | of_property_read_u8(np, "pwr-sel", (u8 *)&pdata->pwr_sel); |
| 591 | |
Milo Kim | ed133352 | 2015-08-24 16:09:55 +0900 | [diff] [blame^] | 592 | return pdata; |
Linus Walleij | 7542a04b | 2013-04-23 04:52:59 -0700 | [diff] [blame] | 593 | } |
| 594 | EXPORT_SYMBOL_GPL(lp55xx_of_populate_pdata); |
| 595 | |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 596 | MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>"); |
| 597 | MODULE_DESCRIPTION("LP55xx Common Driver"); |
| 598 | MODULE_LICENSE("GPL"); |