Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 1 | /* |
| 2 | * LP5521/LP5523/LP55231 Common Driver |
| 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 | |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 15 | #include <linux/delay.h> |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 16 | #include <linux/i2c.h> |
| 17 | #include <linux/leds.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/platform_data/leds-lp55xx.h> |
| 20 | |
| 21 | #include "leds-lp55xx-common.h" |
| 22 | |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 23 | static void lp55xx_reset_device(struct lp55xx_chip *chip) |
| 24 | { |
| 25 | struct lp55xx_device_config *cfg = chip->cfg; |
| 26 | u8 addr = cfg->reset.addr; |
| 27 | u8 val = cfg->reset.val; |
| 28 | |
| 29 | /* no error checking here because no ACK from the device after reset */ |
| 30 | lp55xx_write(chip, addr, val); |
| 31 | } |
| 32 | |
Milo(Woogyom) Kim | e3a700d | 2013-02-05 18:09:56 +0900 | [diff] [blame] | 33 | static int lp55xx_detect_device(struct lp55xx_chip *chip) |
| 34 | { |
| 35 | struct lp55xx_device_config *cfg = chip->cfg; |
| 36 | u8 addr = cfg->enable.addr; |
| 37 | u8 val = cfg->enable.val; |
| 38 | int ret; |
| 39 | |
| 40 | ret = lp55xx_write(chip, addr, val); |
| 41 | if (ret) |
| 42 | return ret; |
| 43 | |
| 44 | usleep_range(1000, 2000); |
| 45 | |
| 46 | ret = lp55xx_read(chip, addr, &val); |
| 47 | if (ret) |
| 48 | return ret; |
| 49 | |
| 50 | if (val != cfg->enable.val) |
| 51 | return -ENODEV; |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 56 | static int lp55xx_post_init_device(struct lp55xx_chip *chip) |
| 57 | { |
| 58 | struct lp55xx_device_config *cfg = chip->cfg; |
| 59 | |
| 60 | if (!cfg->post_init_device) |
| 61 | return 0; |
| 62 | |
| 63 | return cfg->post_init_device(chip); |
| 64 | } |
| 65 | |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame^] | 66 | static int lp55xx_init_led(struct lp55xx_led *led, |
| 67 | struct lp55xx_chip *chip, int chan) |
| 68 | { |
| 69 | return 0; |
| 70 | } |
| 71 | |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 72 | int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val) |
| 73 | { |
| 74 | return i2c_smbus_write_byte_data(chip->cl, reg, val); |
| 75 | } |
| 76 | EXPORT_SYMBOL_GPL(lp55xx_write); |
| 77 | |
| 78 | int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val) |
| 79 | { |
| 80 | s32 ret; |
| 81 | |
| 82 | ret = i2c_smbus_read_byte_data(chip->cl, reg); |
| 83 | if (ret < 0) |
| 84 | return ret; |
| 85 | |
| 86 | *val = ret; |
| 87 | return 0; |
| 88 | } |
| 89 | EXPORT_SYMBOL_GPL(lp55xx_read); |
| 90 | |
| 91 | int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val) |
| 92 | { |
| 93 | int ret; |
| 94 | u8 tmp; |
| 95 | |
| 96 | ret = lp55xx_read(chip, reg, &tmp); |
| 97 | if (ret) |
| 98 | return ret; |
| 99 | |
| 100 | tmp &= ~mask; |
| 101 | tmp |= val & mask; |
| 102 | |
| 103 | return lp55xx_write(chip, reg, tmp); |
| 104 | } |
| 105 | EXPORT_SYMBOL_GPL(lp55xx_update_bits); |
| 106 | |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 107 | int lp55xx_init_device(struct lp55xx_chip *chip) |
| 108 | { |
| 109 | struct lp55xx_platform_data *pdata; |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 110 | struct lp55xx_device_config *cfg; |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 111 | struct device *dev = &chip->cl->dev; |
| 112 | int ret = 0; |
| 113 | |
| 114 | WARN_ON(!chip); |
| 115 | |
| 116 | pdata = chip->pdata; |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 117 | cfg = chip->cfg; |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 118 | |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 119 | if (!pdata || !cfg) |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 120 | return -EINVAL; |
| 121 | |
| 122 | if (pdata->setup_resources) { |
| 123 | ret = pdata->setup_resources(); |
| 124 | if (ret < 0) { |
| 125 | dev_err(dev, "setup resoure err: %d\n", ret); |
| 126 | goto err; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (pdata->enable) { |
| 131 | pdata->enable(0); |
| 132 | usleep_range(1000, 2000); /* Keep enable down at least 1ms */ |
| 133 | pdata->enable(1); |
| 134 | usleep_range(1000, 2000); /* 500us abs min. */ |
| 135 | } |
| 136 | |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 137 | lp55xx_reset_device(chip); |
| 138 | |
| 139 | /* |
| 140 | * Exact value is not available. 10 - 20ms |
| 141 | * appears to be enough for reset. |
| 142 | */ |
| 143 | usleep_range(10000, 20000); |
| 144 | |
Milo(Woogyom) Kim | e3a700d | 2013-02-05 18:09:56 +0900 | [diff] [blame] | 145 | ret = lp55xx_detect_device(chip); |
| 146 | if (ret) { |
| 147 | dev_err(dev, "device detection err: %d\n", ret); |
| 148 | goto err; |
| 149 | } |
| 150 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 151 | /* chip specific initialization */ |
| 152 | ret = lp55xx_post_init_device(chip); |
Milo(Woogyom) Kim | 22ebeb4 | 2013-02-05 18:58:35 +0900 | [diff] [blame] | 153 | if (ret) { |
| 154 | dev_err(dev, "post init device err: %d\n", ret); |
| 155 | goto err_post_init; |
| 156 | } |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 157 | |
| 158 | return 0; |
| 159 | |
Milo(Woogyom) Kim | 22ebeb4 | 2013-02-05 18:58:35 +0900 | [diff] [blame] | 160 | err_post_init: |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 161 | lp55xx_deinit_device(chip); |
Milo(Woogyom) Kim | a85908d | 2013-02-05 18:07:20 +0900 | [diff] [blame] | 162 | err: |
| 163 | return ret; |
| 164 | } |
| 165 | EXPORT_SYMBOL_GPL(lp55xx_init_device); |
| 166 | |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 167 | void lp55xx_deinit_device(struct lp55xx_chip *chip) |
| 168 | { |
| 169 | struct lp55xx_platform_data *pdata = chip->pdata; |
| 170 | |
| 171 | if (pdata->enable) |
| 172 | pdata->enable(0); |
| 173 | |
| 174 | if (pdata->release_resources) |
| 175 | pdata->release_resources(); |
| 176 | } |
| 177 | EXPORT_SYMBOL_GPL(lp55xx_deinit_device); |
| 178 | |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame^] | 179 | int lp55xx_register_leds(struct lp55xx_led *led, struct lp55xx_chip *chip) |
| 180 | { |
| 181 | struct lp55xx_platform_data *pdata = chip->pdata; |
| 182 | struct lp55xx_device_config *cfg = chip->cfg; |
| 183 | int num_channels = pdata->num_channels; |
| 184 | struct lp55xx_led *each; |
| 185 | u8 led_current; |
| 186 | int ret; |
| 187 | int i; |
| 188 | |
| 189 | if (!cfg->brightness_work_fn) { |
| 190 | dev_err(&chip->cl->dev, "empty brightness configuration\n"); |
| 191 | return -EINVAL; |
| 192 | } |
| 193 | |
| 194 | for (i = 0; i < num_channels; i++) { |
| 195 | |
| 196 | /* do not initialize channels that are not connected */ |
| 197 | if (pdata->led_config[i].led_current == 0) |
| 198 | continue; |
| 199 | |
| 200 | led_current = pdata->led_config[i].led_current; |
| 201 | each = led + i; |
| 202 | ret = lp55xx_init_led(each, chip, i); |
| 203 | if (ret) |
| 204 | goto err_init_led; |
| 205 | |
| 206 | INIT_WORK(&each->brightness_work, cfg->brightness_work_fn); |
| 207 | |
| 208 | chip->num_leds++; |
| 209 | each->chip = chip; |
| 210 | |
| 211 | /* setting led current at each channel */ |
| 212 | if (cfg->set_led_current) |
| 213 | cfg->set_led_current(each, led_current); |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | |
| 218 | err_init_led: |
| 219 | for (i = 0; i < chip->num_leds; i++) { |
| 220 | each = led + i; |
| 221 | led_classdev_unregister(&each->cdev); |
| 222 | flush_work(&each->brightness_work); |
| 223 | } |
| 224 | return ret; |
| 225 | } |
| 226 | EXPORT_SYMBOL_GPL(lp55xx_register_leds); |
| 227 | |
Milo(Woogyom) Kim | c93d08f | 2013-02-05 18:01:23 +0900 | [diff] [blame] | 228 | MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>"); |
| 229 | MODULE_DESCRIPTION("LP55xx Common Driver"); |
| 230 | MODULE_LICENSE("GPL"); |