Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * lp5523.c - LP5523 LED Driver |
| 3 | * |
| 4 | * Copyright (C) 2010 Nokia Corporation |
| 5 | * |
| 6 | * Contact: Samu Onkalo <samu.p.onkalo@nokia.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * version 2 as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 20 | * 02110-1301 USA |
| 21 | */ |
| 22 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 23 | #include <linux/delay.h> |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 24 | #include <linux/firmware.h> |
Milo(Woogyom) Kim | 79bcc10 | 2013-02-05 19:26:14 +0900 | [diff] [blame^] | 25 | #include <linux/i2c.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/leds.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/mutex.h> |
| 30 | #include <linux/platform_data/leds-lp55xx.h> |
| 31 | #include <linux/slab.h> |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 32 | |
| 33 | #include "leds-lp55xx-common.h" |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 34 | |
Milo(Woogyom) Kim | 12f022d | 2013-02-05 19:25:26 +0900 | [diff] [blame] | 35 | #define LP5523_PROGRAM_LENGTH 32 |
| 36 | #define LP5523_MAX_LEDS 9 |
| 37 | |
| 38 | /* Registers */ |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 39 | #define LP5523_REG_ENABLE 0x00 |
| 40 | #define LP5523_REG_OP_MODE 0x01 |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 41 | #define LP5523_REG_ENABLE_LEDS_MSB 0x04 |
| 42 | #define LP5523_REG_ENABLE_LEDS_LSB 0x05 |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 43 | #define LP5523_REG_LED_PWM_BASE 0x16 |
| 44 | #define LP5523_REG_LED_CURRENT_BASE 0x26 |
| 45 | #define LP5523_REG_CONFIG 0x36 |
Milo(Woogyom) Kim | 12f022d | 2013-02-05 19:25:26 +0900 | [diff] [blame] | 46 | #define LP5523_REG_STATUS 0x3A |
| 47 | #define LP5523_REG_RESET 0x3D |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 48 | #define LP5523_REG_LED_TEST_CTRL 0x41 |
| 49 | #define LP5523_REG_LED_TEST_ADC 0x42 |
Milo(Woogyom) Kim | 12f022d | 2013-02-05 19:25:26 +0900 | [diff] [blame] | 50 | #define LP5523_REG_PROG_PAGE_SEL 0x4F |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 51 | #define LP5523_REG_PROG_MEM 0x50 |
| 52 | |
Milo(Woogyom) Kim | 12f022d | 2013-02-05 19:25:26 +0900 | [diff] [blame] | 53 | /* Bit description in registers */ |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 54 | #define LP5523_ENABLE 0x40 |
| 55 | #define LP5523_AUTO_INC 0x40 |
| 56 | #define LP5523_PWR_SAVE 0x20 |
| 57 | #define LP5523_PWM_PWR_SAVE 0x04 |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 58 | #define LP5523_CP_AUTO 0x18 |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 59 | #define LP5523_AUTO_CLK 0x02 |
Milo(Woogyom) Kim | 12f022d | 2013-02-05 19:25:26 +0900 | [diff] [blame] | 60 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 61 | #define LP5523_EN_LEDTEST 0x80 |
| 62 | #define LP5523_LEDTEST_DONE 0x80 |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 63 | #define LP5523_RESET 0xFF |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 64 | #define LP5523_ADC_SHORTCIRC_LIM 80 |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 65 | #define LP5523_EXT_CLK_USED 0x08 |
| 66 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 67 | /* Memory Page Selection */ |
| 68 | #define LP5523_PAGE_ENG1 0 |
| 69 | #define LP5523_PAGE_ENG2 1 |
| 70 | #define LP5523_PAGE_ENG3 2 |
| 71 | |
| 72 | /* Program Memory Operations */ |
| 73 | #define LP5523_MODE_ENG1_M 0x30 /* Operation Mode Register */ |
| 74 | #define LP5523_MODE_ENG2_M 0x0C |
| 75 | #define LP5523_MODE_ENG3_M 0x03 |
| 76 | #define LP5523_LOAD_ENG1 0x10 |
| 77 | #define LP5523_LOAD_ENG2 0x04 |
| 78 | #define LP5523_LOAD_ENG3 0x01 |
| 79 | |
| 80 | #define LP5523_ENG1_IS_LOADING(mode) \ |
| 81 | ((mode & LP5523_MODE_ENG1_M) == LP5523_LOAD_ENG1) |
| 82 | #define LP5523_ENG2_IS_LOADING(mode) \ |
| 83 | ((mode & LP5523_MODE_ENG2_M) == LP5523_LOAD_ENG2) |
| 84 | #define LP5523_ENG3_IS_LOADING(mode) \ |
| 85 | ((mode & LP5523_MODE_ENG3_M) == LP5523_LOAD_ENG3) |
| 86 | |
| 87 | #define LP5523_EXEC_ENG1_M 0x30 /* Enable Register */ |
| 88 | #define LP5523_EXEC_ENG2_M 0x0C |
| 89 | #define LP5523_EXEC_ENG3_M 0x03 |
| 90 | #define LP5523_EXEC_M 0x3F |
| 91 | #define LP5523_RUN_ENG1 0x20 |
| 92 | #define LP5523_RUN_ENG2 0x08 |
| 93 | #define LP5523_RUN_ENG3 0x02 |
| 94 | |
Kim, Milo | 27d7704 | 2012-09-04 15:06:18 +0800 | [diff] [blame] | 95 | enum lp5523_chip_id { |
| 96 | LP5523, |
| 97 | LP55231, |
| 98 | }; |
| 99 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 100 | static inline void lp5523_wait_opmode_done(void) |
| 101 | { |
| 102 | usleep_range(1000, 2000); |
| 103 | } |
| 104 | |
Milo(Woogyom) Kim | a96bfa1 | 2013-02-05 19:09:32 +0900 | [diff] [blame] | 105 | static void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current) |
| 106 | { |
| 107 | led->led_current = led_current; |
| 108 | lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr, |
| 109 | led_current); |
| 110 | } |
| 111 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 112 | static int lp5523_post_init_device(struct lp55xx_chip *chip) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 113 | { |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 114 | int ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 115 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 116 | ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE); |
Milo(Woogyom) Kim | 632418b | 2013-02-05 18:04:50 +0900 | [diff] [blame] | 117 | if (ret) |
| 118 | return ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 119 | |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 120 | /* Chip startup time is 500 us, 1 - 2 ms gives some margin */ |
| 121 | usleep_range(1000, 2000); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 122 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 123 | ret = lp55xx_write(chip, LP5523_REG_CONFIG, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 124 | LP5523_AUTO_INC | LP5523_PWR_SAVE | |
| 125 | LP5523_CP_AUTO | LP5523_AUTO_CLK | |
| 126 | LP5523_PWM_PWR_SAVE); |
Milo(Woogyom) Kim | 632418b | 2013-02-05 18:04:50 +0900 | [diff] [blame] | 127 | if (ret) |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 128 | return ret; |
| 129 | |
Milo(Woogyom) Kim | 632418b | 2013-02-05 18:04:50 +0900 | [diff] [blame] | 130 | /* turn on all leds */ |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 131 | ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01); |
Milo(Woogyom) Kim | 632418b | 2013-02-05 18:04:50 +0900 | [diff] [blame] | 132 | if (ret) |
| 133 | return ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 134 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 135 | return lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 138 | static void lp5523_load_engine(struct lp55xx_chip *chip) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 139 | { |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 140 | enum lp55xx_engine_index idx = chip->engine_idx; |
| 141 | u8 mask[] = { |
| 142 | [LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M, |
| 143 | [LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M, |
| 144 | [LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M, |
| 145 | }; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 146 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 147 | u8 val[] = { |
| 148 | [LP55XX_ENGINE_1] = LP5523_LOAD_ENG1, |
| 149 | [LP55XX_ENGINE_2] = LP5523_LOAD_ENG2, |
| 150 | [LP55XX_ENGINE_3] = LP5523_LOAD_ENG3, |
| 151 | }; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 152 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 153 | u8 page_sel[] = { |
| 154 | [LP55XX_ENGINE_1] = LP5523_PAGE_ENG1, |
| 155 | [LP55XX_ENGINE_2] = LP5523_PAGE_ENG2, |
| 156 | [LP55XX_ENGINE_3] = LP5523_PAGE_ENG3, |
| 157 | }; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 158 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 159 | lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 160 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 161 | lp5523_wait_opmode_done(); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 162 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 163 | lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 166 | static void lp5523_stop_engine(struct lp55xx_chip *chip) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 167 | { |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 168 | lp55xx_write(chip, LP5523_REG_OP_MODE, 0); |
| 169 | lp5523_wait_opmode_done(); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 172 | static void lp5523_turn_off_channels(struct lp55xx_chip *chip) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 173 | { |
| 174 | int i; |
Kim, Milo | 469eba0 | 2012-08-21 17:04:02 +0800 | [diff] [blame] | 175 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 176 | for (i = 0; i < LP5523_MAX_LEDS; i++) |
| 177 | lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0); |
| 178 | } |
| 179 | |
| 180 | static void lp5523_run_engine(struct lp55xx_chip *chip, bool start) |
| 181 | { |
| 182 | int ret; |
| 183 | u8 mode; |
| 184 | u8 exec; |
| 185 | |
| 186 | /* stop engine */ |
| 187 | if (!start) { |
| 188 | lp5523_stop_engine(chip); |
| 189 | lp5523_turn_off_channels(chip); |
| 190 | return; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 191 | } |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 192 | |
| 193 | /* |
| 194 | * To run the engine, |
| 195 | * operation mode and enable register should updated at the same time |
| 196 | */ |
| 197 | |
| 198 | ret = lp55xx_read(chip, LP5523_REG_OP_MODE, &mode); |
| 199 | if (ret) |
| 200 | return; |
| 201 | |
| 202 | ret = lp55xx_read(chip, LP5523_REG_ENABLE, &exec); |
| 203 | if (ret) |
| 204 | return; |
| 205 | |
| 206 | /* change operation mode to RUN only when each engine is loading */ |
| 207 | if (LP5523_ENG1_IS_LOADING(mode)) { |
| 208 | mode = (mode & ~LP5523_MODE_ENG1_M) | LP5523_RUN_ENG1; |
| 209 | exec = (exec & ~LP5523_EXEC_ENG1_M) | LP5523_RUN_ENG1; |
| 210 | } |
| 211 | |
| 212 | if (LP5523_ENG2_IS_LOADING(mode)) { |
| 213 | mode = (mode & ~LP5523_MODE_ENG2_M) | LP5523_RUN_ENG2; |
| 214 | exec = (exec & ~LP5523_EXEC_ENG2_M) | LP5523_RUN_ENG2; |
| 215 | } |
| 216 | |
| 217 | if (LP5523_ENG3_IS_LOADING(mode)) { |
| 218 | mode = (mode & ~LP5523_MODE_ENG3_M) | LP5523_RUN_ENG3; |
| 219 | exec = (exec & ~LP5523_EXEC_ENG3_M) | LP5523_RUN_ENG3; |
| 220 | } |
| 221 | |
| 222 | lp55xx_write(chip, LP5523_REG_OP_MODE, mode); |
| 223 | lp5523_wait_opmode_done(); |
| 224 | |
| 225 | lp55xx_update_bits(chip, LP5523_REG_ENABLE, LP5523_EXEC_M, exec); |
| 226 | } |
| 227 | |
| 228 | static int lp5523_update_program_memory(struct lp55xx_chip *chip, |
| 229 | const u8 *data, size_t size) |
| 230 | { |
| 231 | u8 pattern[LP5523_PROGRAM_LENGTH] = {0}; |
| 232 | unsigned cmd; |
| 233 | char c[3]; |
| 234 | int update_size; |
| 235 | int nrchars; |
| 236 | int offset = 0; |
| 237 | int ret; |
| 238 | int i; |
| 239 | |
| 240 | /* clear program memory before updating */ |
| 241 | for (i = 0; i < LP5523_PROGRAM_LENGTH; i++) |
| 242 | lp55xx_write(chip, LP5523_REG_PROG_MEM + i, 0); |
| 243 | |
| 244 | i = 0; |
| 245 | while ((offset < size - 1) && (i < LP5523_PROGRAM_LENGTH)) { |
| 246 | /* separate sscanfs because length is working only for %s */ |
| 247 | ret = sscanf(data + offset, "%2s%n ", c, &nrchars); |
| 248 | if (ret != 1) |
| 249 | goto err; |
| 250 | |
| 251 | ret = sscanf(c, "%2x", &cmd); |
| 252 | if (ret != 1) |
| 253 | goto err; |
| 254 | |
| 255 | pattern[i] = (u8)cmd; |
| 256 | offset += nrchars; |
| 257 | i++; |
| 258 | } |
| 259 | |
| 260 | /* Each instruction is 16bit long. Check that length is even */ |
| 261 | if (i % 2) |
| 262 | goto err; |
| 263 | |
| 264 | update_size = i; |
| 265 | for (i = 0; i < update_size; i++) |
| 266 | lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 267 | |
| 268 | return 0; |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 269 | |
| 270 | err: |
| 271 | dev_err(&chip->cl->dev, "wrong pattern format\n"); |
| 272 | return -EINVAL; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 275 | static void lp5523_firmware_loaded(struct lp55xx_chip *chip) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 276 | { |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 277 | const struct firmware *fw = chip->fw; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 278 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 279 | if (fw->size > LP5523_PROGRAM_LENGTH) { |
| 280 | dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n", |
| 281 | fw->size); |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Program momery sequence |
| 287 | * 1) set engine mode to "LOAD" |
| 288 | * 2) write firmware data into program memory |
| 289 | */ |
| 290 | |
| 291 | lp5523_load_engine(chip); |
| 292 | lp5523_update_program_memory(chip, fw->data, fw->size); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 295 | static ssize_t lp5523_selftest(struct device *dev, |
| 296 | struct device_attribute *attr, |
| 297 | char *buf) |
| 298 | { |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 299 | struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev)); |
| 300 | struct lp55xx_chip *chip = led->chip; |
| 301 | struct lp55xx_platform_data *pdata = chip->pdata; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 302 | int i, ret, pos = 0; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 303 | u8 status, adc, vdd; |
| 304 | |
| 305 | mutex_lock(&chip->lock); |
| 306 | |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 307 | ret = lp55xx_read(chip, LP5523_REG_STATUS, &status); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 308 | if (ret < 0) |
| 309 | goto fail; |
| 310 | |
| 311 | /* Check that ext clock is really in use if requested */ |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 312 | if (pdata->clock_mode == LP55XX_CLOCK_EXT) { |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 313 | if ((status & LP5523_EXT_CLK_USED) == 0) |
| 314 | goto fail; |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 315 | } |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 316 | |
| 317 | /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */ |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 318 | lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, LP5523_EN_LEDTEST | 16); |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 319 | usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */ |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 320 | ret = lp55xx_read(chip, LP5523_REG_STATUS, &status); |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 321 | if (ret < 0) |
| 322 | goto fail; |
| 323 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 324 | if (!(status & LP5523_LEDTEST_DONE)) |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 325 | usleep_range(3000, 6000); /* Was not ready. Wait little bit */ |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 326 | |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 327 | ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &vdd); |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 328 | if (ret < 0) |
| 329 | goto fail; |
| 330 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 331 | vdd--; /* There may be some fluctuation in measurement */ |
| 332 | |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 333 | for (i = 0; i < LP5523_MAX_LEDS; i++) { |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 334 | /* Skip non-existing channels */ |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 335 | if (pdata->led_config[i].led_current == 0) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 336 | continue; |
| 337 | |
| 338 | /* Set default current */ |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 339 | lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i, |
| 340 | pdata->led_config[i].led_current); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 341 | |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 342 | lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0xff); |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 343 | /* let current stabilize 2 - 4ms before measurements start */ |
| 344 | usleep_range(2000, 4000); |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 345 | lp55xx_write(chip, LP5523_REG_LED_TEST_CTRL, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 346 | LP5523_EN_LEDTEST | i); |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 347 | /* ADC conversion time is 2.7 ms typically */ |
| 348 | usleep_range(3000, 6000); |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 349 | ret = lp55xx_read(chip, LP5523_REG_STATUS, &status); |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 350 | if (ret < 0) |
| 351 | goto fail; |
| 352 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 353 | if (!(status & LP5523_LEDTEST_DONE)) |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 354 | usleep_range(3000, 6000);/* Was not ready. Wait. */ |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 355 | |
| 356 | ret = lp55xx_read(chip, LP5523_REG_LED_TEST_ADC, &adc); |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 357 | if (ret < 0) |
| 358 | goto fail; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 359 | |
| 360 | if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM) |
| 361 | pos += sprintf(buf + pos, "LED %d FAIL\n", i); |
| 362 | |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 363 | lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0x00); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 364 | |
| 365 | /* Restore current */ |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 366 | lp55xx_write(chip, LP5523_REG_LED_CURRENT_BASE + i, |
| 367 | led->led_current); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 368 | led++; |
| 369 | } |
| 370 | if (pos == 0) |
| 371 | pos = sprintf(buf, "OK\n"); |
| 372 | goto release_lock; |
| 373 | fail: |
| 374 | pos = sprintf(buf, "FAIL\n"); |
| 375 | |
| 376 | release_lock: |
| 377 | mutex_unlock(&chip->lock); |
| 378 | |
| 379 | return pos; |
| 380 | } |
| 381 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 382 | static void lp5523_led_brightness_work(struct work_struct *work) |
| 383 | { |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame] | 384 | struct lp55xx_led *led = container_of(work, struct lp55xx_led, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 385 | brightness_work); |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame] | 386 | struct lp55xx_chip *chip = led->chip; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 387 | |
| 388 | mutex_lock(&chip->lock); |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame] | 389 | lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 390 | led->brightness); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 391 | mutex_unlock(&chip->lock); |
| 392 | } |
| 393 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 394 | static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL); |
| 395 | |
| 396 | static struct attribute *lp5523_attributes[] = { |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 397 | &dev_attr_selftest.attr, |
Kim, Milo | f162584 | 2012-09-12 20:16:03 +0800 | [diff] [blame] | 398 | NULL, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 399 | }; |
| 400 | |
| 401 | static const struct attribute_group lp5523_group = { |
| 402 | .attrs = lp5523_attributes, |
| 403 | }; |
| 404 | |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 405 | /* Chip specific configurations */ |
| 406 | static struct lp55xx_device_config lp5523_cfg = { |
| 407 | .reset = { |
| 408 | .addr = LP5523_REG_RESET, |
| 409 | .val = LP5523_RESET, |
| 410 | }, |
Milo(Woogyom) Kim | e3a700d | 2013-02-05 18:09:56 +0900 | [diff] [blame] | 411 | .enable = { |
| 412 | .addr = LP5523_REG_ENABLE, |
| 413 | .val = LP5523_ENABLE, |
| 414 | }, |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 415 | .max_channel = LP5523_MAX_LEDS, |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 416 | .post_init_device = lp5523_post_init_device, |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame] | 417 | .brightness_work_fn = lp5523_led_brightness_work, |
Milo(Woogyom) Kim | a96bfa1 | 2013-02-05 19:09:32 +0900 | [diff] [blame] | 418 | .set_led_current = lp5523_set_led_current, |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame] | 419 | .firmware_cb = lp5523_firmware_loaded, |
| 420 | .run_engine = lp5523_run_engine, |
Milo(Woogyom) Kim | e73c0ce | 2013-02-05 19:20:45 +0900 | [diff] [blame] | 421 | .dev_attr_group = &lp5523_group, |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 422 | }; |
| 423 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 424 | static int lp5523_probe(struct i2c_client *client, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 425 | const struct i2c_device_id *id) |
| 426 | { |
Milo(Woogyom) Kim | 22ebeb4 | 2013-02-05 18:58:35 +0900 | [diff] [blame] | 427 | int ret; |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 428 | struct lp55xx_chip *chip; |
| 429 | struct lp55xx_led *led; |
| 430 | struct lp55xx_platform_data *pdata = client->dev.platform_data; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 431 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 432 | if (!pdata) { |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 433 | dev_err(&client->dev, "no platform data\n"); |
Bryan Wu | 94ca4bc | 2012-07-04 11:44:18 +0800 | [diff] [blame] | 434 | return -EINVAL; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 437 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); |
| 438 | if (!chip) |
| 439 | return -ENOMEM; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 440 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 441 | led = devm_kzalloc(&client->dev, |
| 442 | sizeof(*led) * pdata->num_channels, GFP_KERNEL); |
| 443 | if (!led) |
| 444 | return -ENOMEM; |
| 445 | |
| 446 | chip->cl = client; |
| 447 | chip->pdata = pdata; |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 448 | chip->cfg = &lp5523_cfg; |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 449 | |
| 450 | mutex_init(&chip->lock); |
| 451 | |
| 452 | i2c_set_clientdata(client, led); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 453 | |
Milo(Woogyom) Kim | 22ebeb4 | 2013-02-05 18:58:35 +0900 | [diff] [blame] | 454 | ret = lp55xx_init_device(chip); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 455 | if (ret) |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 456 | goto err_init; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 457 | |
Kim, Milo | 56a1e9a | 2012-09-04 15:06:26 +0800 | [diff] [blame] | 458 | dev_info(&client->dev, "%s Programmable led chip found\n", id->name); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 459 | |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 460 | ret = lp55xx_register_leds(led, chip); |
Milo(Woogyom) Kim | f652480 | 2013-02-05 17:53:40 +0900 | [diff] [blame] | 461 | if (ret) |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 462 | goto err_register_leds; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 463 | |
Milo(Woogyom) Kim | e73c0ce | 2013-02-05 19:20:45 +0900 | [diff] [blame] | 464 | ret = lp55xx_register_sysfs(chip); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 465 | if (ret) { |
| 466 | dev_err(&client->dev, "registering sysfs failed\n"); |
Milo(Woogyom) Kim | e73c0ce | 2013-02-05 19:20:45 +0900 | [diff] [blame] | 467 | goto err_register_sysfs; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 468 | } |
Milo(Woogyom) Kim | e73c0ce | 2013-02-05 19:20:45 +0900 | [diff] [blame] | 469 | |
| 470 | return 0; |
| 471 | |
| 472 | err_register_sysfs: |
Milo(Woogyom) Kim | c3a68eb | 2013-02-05 19:11:18 +0900 | [diff] [blame] | 473 | lp55xx_unregister_leds(led, chip); |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 474 | err_register_leds: |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 475 | lp55xx_deinit_device(chip); |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 476 | err_init: |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | static int lp5523_remove(struct i2c_client *client) |
| 481 | { |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 482 | struct lp55xx_led *led = i2c_get_clientdata(client); |
| 483 | struct lp55xx_chip *chip = led->chip; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 484 | |
Milo(Woogyom) Kim | 87cc4bd | 2013-02-05 19:23:51 +0900 | [diff] [blame] | 485 | lp5523_stop_engine(chip); |
| 486 | lp55xx_unregister_sysfs(chip); |
Milo(Woogyom) Kim | c3a68eb | 2013-02-05 19:11:18 +0900 | [diff] [blame] | 487 | lp55xx_unregister_leds(led, chip); |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 488 | lp55xx_deinit_device(chip); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 489 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | static const struct i2c_device_id lp5523_id[] = { |
Kim, Milo | 27d7704 | 2012-09-04 15:06:18 +0800 | [diff] [blame] | 494 | { "lp5523", LP5523 }, |
| 495 | { "lp55231", LP55231 }, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 496 | { } |
| 497 | }; |
| 498 | |
| 499 | MODULE_DEVICE_TABLE(i2c, lp5523_id); |
| 500 | |
| 501 | static struct i2c_driver lp5523_driver = { |
| 502 | .driver = { |
Kim, Milo | 27d7704 | 2012-09-04 15:06:18 +0800 | [diff] [blame] | 503 | .name = "lp5523x", |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 504 | }, |
| 505 | .probe = lp5523_probe, |
| 506 | .remove = lp5523_remove, |
| 507 | .id_table = lp5523_id, |
| 508 | }; |
| 509 | |
Axel Lin | 09a0d18 | 2012-01-10 15:09:27 -0800 | [diff] [blame] | 510 | module_i2c_driver(lp5523_driver); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 511 | |
| 512 | MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>"); |
| 513 | MODULE_DESCRIPTION("LP5523 LED engine"); |
| 514 | MODULE_LICENSE("GPL"); |