Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * LP5521 LED chip 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 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/i2c.h> |
| 26 | #include <linux/mutex.h> |
| 27 | #include <linux/gpio.h> |
| 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/ctype.h> |
| 31 | #include <linux/spinlock.h> |
| 32 | #include <linux/wait.h> |
| 33 | #include <linux/leds.h> |
| 34 | #include <linux/leds-lp5521.h> |
| 35 | #include <linux/workqueue.h> |
| 36 | #include <linux/slab.h> |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 37 | #include <linux/platform_data/leds-lp55xx.h> |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 38 | #include <linux/firmware.h> |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 39 | |
| 40 | #include "leds-lp55xx-common.h" |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 41 | |
Milo(Woogyom) Kim | 12f022d | 2013-02-05 19:25:26 +0900 | [diff] [blame^] | 42 | #define LP5521_PROGRAM_LENGTH 32 |
| 43 | #define LP5521_MAX_LEDS 3 |
| 44 | #define LP5521_CMD_DIRECT 0x3F |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 45 | |
| 46 | /* Registers */ |
| 47 | #define LP5521_REG_ENABLE 0x00 |
| 48 | #define LP5521_REG_OP_MODE 0x01 |
| 49 | #define LP5521_REG_R_PWM 0x02 |
| 50 | #define LP5521_REG_G_PWM 0x03 |
| 51 | #define LP5521_REG_B_PWM 0x04 |
| 52 | #define LP5521_REG_R_CURRENT 0x05 |
| 53 | #define LP5521_REG_G_CURRENT 0x06 |
| 54 | #define LP5521_REG_B_CURRENT 0x07 |
| 55 | #define LP5521_REG_CONFIG 0x08 |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 56 | #define LP5521_REG_STATUS 0x0C |
| 57 | #define LP5521_REG_RESET 0x0D |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 58 | #define LP5521_REG_R_PROG_MEM 0x10 |
| 59 | #define LP5521_REG_G_PROG_MEM 0x30 |
| 60 | #define LP5521_REG_B_PROG_MEM 0x50 |
| 61 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 62 | /* Base register to set LED current */ |
| 63 | #define LP5521_REG_LED_CURRENT_BASE LP5521_REG_R_CURRENT |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 64 | /* Base register to set the brightness */ |
| 65 | #define LP5521_REG_LED_PWM_BASE LP5521_REG_R_PWM |
| 66 | |
| 67 | /* Bits in ENABLE register */ |
| 68 | #define LP5521_MASTER_ENABLE 0x40 /* Chip master enable */ |
| 69 | #define LP5521_LOGARITHMIC_PWM 0x80 /* Logarithmic PWM adjustment */ |
| 70 | #define LP5521_EXEC_RUN 0x2A |
Kim, Milo | 32a2f74 | 2012-03-23 15:02:09 -0700 | [diff] [blame] | 71 | #define LP5521_ENABLE_DEFAULT \ |
| 72 | (LP5521_MASTER_ENABLE | LP5521_LOGARITHMIC_PWM) |
| 73 | #define LP5521_ENABLE_RUN_PROGRAM \ |
| 74 | (LP5521_ENABLE_DEFAULT | LP5521_EXEC_RUN) |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 75 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 76 | /* Status */ |
| 77 | #define LP5521_EXT_CLK_USED 0x08 |
| 78 | |
Srinidhi KASAGAR | b3c49c0 | 2011-10-31 17:12:24 -0700 | [diff] [blame] | 79 | /* default R channel current register value */ |
| 80 | #define LP5521_REG_R_CURR_DEFAULT 0xAF |
| 81 | |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 82 | /* Reset register value */ |
| 83 | #define LP5521_RESET 0xFF |
| 84 | |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 85 | /* Program Memory Operations */ |
| 86 | #define LP5521_MODE_R_M 0x30 /* Operation Mode Register */ |
| 87 | #define LP5521_MODE_G_M 0x0C |
| 88 | #define LP5521_MODE_B_M 0x03 |
| 89 | #define LP5521_LOAD_R 0x10 |
| 90 | #define LP5521_LOAD_G 0x04 |
| 91 | #define LP5521_LOAD_B 0x01 |
| 92 | |
| 93 | #define LP5521_R_IS_LOADING(mode) \ |
| 94 | ((mode & LP5521_MODE_R_M) == LP5521_LOAD_R) |
| 95 | #define LP5521_G_IS_LOADING(mode) \ |
| 96 | ((mode & LP5521_MODE_G_M) == LP5521_LOAD_G) |
| 97 | #define LP5521_B_IS_LOADING(mode) \ |
| 98 | ((mode & LP5521_MODE_B_M) == LP5521_LOAD_B) |
| 99 | |
| 100 | #define LP5521_EXEC_R_M 0x30 /* Enable Register */ |
| 101 | #define LP5521_EXEC_G_M 0x0C |
| 102 | #define LP5521_EXEC_B_M 0x03 |
| 103 | #define LP5521_EXEC_M 0x3F |
| 104 | #define LP5521_RUN_R 0x20 |
| 105 | #define LP5521_RUN_G 0x08 |
| 106 | #define LP5521_RUN_B 0x02 |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 107 | |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 108 | static inline void lp5521_wait_opmode_done(void) |
| 109 | { |
| 110 | /* operation mode change needs to be longer than 153 us */ |
| 111 | usleep_range(200, 300); |
| 112 | } |
| 113 | |
Milo(Woogyom) Kim | 9448217 | 2013-02-05 18:03:55 +0900 | [diff] [blame] | 114 | static inline void lp5521_wait_enable_done(void) |
| 115 | { |
| 116 | /* it takes more 488 us to update ENABLE register */ |
| 117 | usleep_range(500, 600); |
| 118 | } |
| 119 | |
Milo(Woogyom) Kim | a96bfa1 | 2013-02-05 19:09:32 +0900 | [diff] [blame] | 120 | static void lp5521_set_led_current(struct lp55xx_led *led, u8 led_current) |
| 121 | { |
| 122 | led->led_current = led_current; |
| 123 | lp55xx_write(led->chip, LP5521_REG_LED_CURRENT_BASE + led->chan_nr, |
| 124 | led_current); |
| 125 | } |
| 126 | |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 127 | static void lp5521_load_engine(struct lp55xx_chip *chip) |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 128 | { |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 129 | enum lp55xx_engine_index idx = chip->engine_idx; |
| 130 | u8 mask[] = { |
| 131 | [LP55XX_ENGINE_1] = LP5521_MODE_R_M, |
| 132 | [LP55XX_ENGINE_2] = LP5521_MODE_G_M, |
| 133 | [LP55XX_ENGINE_3] = LP5521_MODE_B_M, |
| 134 | }; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 135 | |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 136 | u8 val[] = { |
| 137 | [LP55XX_ENGINE_1] = LP5521_LOAD_R, |
| 138 | [LP55XX_ENGINE_2] = LP5521_LOAD_G, |
| 139 | [LP55XX_ENGINE_3] = LP5521_LOAD_B, |
| 140 | }; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 141 | |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 142 | lp55xx_update_bits(chip, LP5521_REG_OP_MODE, mask[idx], val[idx]); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 143 | |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 144 | lp5521_wait_opmode_done(); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 147 | static void lp5521_stop_engine(struct lp55xx_chip *chip) |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 148 | { |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 149 | lp55xx_write(chip, LP5521_REG_OP_MODE, 0); |
| 150 | lp5521_wait_opmode_done(); |
| 151 | } |
| 152 | |
| 153 | static void lp5521_run_engine(struct lp55xx_chip *chip, bool start) |
| 154 | { |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 155 | int ret; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 156 | u8 mode; |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 157 | u8 exec; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 158 | |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 159 | /* stop engine */ |
| 160 | if (!start) { |
| 161 | lp5521_stop_engine(chip); |
| 162 | lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT); |
| 163 | lp5521_wait_opmode_done(); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * To run the engine, |
| 169 | * operation mode and enable register should updated at the same time |
| 170 | */ |
| 171 | |
| 172 | ret = lp55xx_read(chip, LP5521_REG_OP_MODE, &mode); |
Dan Carpenter | 5bc9ad7 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 173 | if (ret) |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 174 | return; |
Dan Carpenter | 5bc9ad7 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 175 | |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 176 | ret = lp55xx_read(chip, LP5521_REG_ENABLE, &exec); |
Dan Carpenter | 5bc9ad7 | 2012-05-29 15:07:26 -0700 | [diff] [blame] | 177 | if (ret) |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 178 | return; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 179 | |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 180 | /* change operation mode to RUN only when each engine is loading */ |
| 181 | if (LP5521_R_IS_LOADING(mode)) { |
| 182 | mode = (mode & ~LP5521_MODE_R_M) | LP5521_RUN_R; |
| 183 | exec = (exec & ~LP5521_EXEC_R_M) | LP5521_RUN_R; |
| 184 | } |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 185 | |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 186 | if (LP5521_G_IS_LOADING(mode)) { |
| 187 | mode = (mode & ~LP5521_MODE_G_M) | LP5521_RUN_G; |
| 188 | exec = (exec & ~LP5521_EXEC_G_M) | LP5521_RUN_G; |
| 189 | } |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 190 | |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 191 | if (LP5521_B_IS_LOADING(mode)) { |
| 192 | mode = (mode & ~LP5521_MODE_B_M) | LP5521_RUN_B; |
| 193 | exec = (exec & ~LP5521_EXEC_B_M) | LP5521_RUN_B; |
| 194 | } |
| 195 | |
| 196 | lp55xx_write(chip, LP5521_REG_OP_MODE, mode); |
| 197 | lp5521_wait_opmode_done(); |
| 198 | |
| 199 | lp55xx_update_bits(chip, LP5521_REG_ENABLE, LP5521_EXEC_M, exec); |
| 200 | lp5521_wait_enable_done(); |
| 201 | } |
| 202 | |
| 203 | static int lp5521_update_program_memory(struct lp55xx_chip *chip, |
| 204 | const u8 *data, size_t size) |
| 205 | { |
| 206 | enum lp55xx_engine_index idx = chip->engine_idx; |
| 207 | u8 pattern[LP5521_PROGRAM_LENGTH] = {0}; |
| 208 | u8 addr[] = { |
| 209 | [LP55XX_ENGINE_1] = LP5521_REG_R_PROG_MEM, |
| 210 | [LP55XX_ENGINE_2] = LP5521_REG_G_PROG_MEM, |
| 211 | [LP55XX_ENGINE_3] = LP5521_REG_B_PROG_MEM, |
| 212 | }; |
| 213 | unsigned cmd; |
| 214 | char c[3]; |
| 215 | int program_size; |
| 216 | int nrchars; |
| 217 | int offset = 0; |
| 218 | int ret; |
| 219 | int i; |
| 220 | |
| 221 | /* clear program memory before updating */ |
| 222 | for (i = 0; i < LP5521_PROGRAM_LENGTH; i++) |
| 223 | lp55xx_write(chip, addr[idx] + i, 0); |
| 224 | |
| 225 | i = 0; |
| 226 | while ((offset < size - 1) && (i < LP5521_PROGRAM_LENGTH)) { |
| 227 | /* separate sscanfs because length is working only for %s */ |
| 228 | ret = sscanf(data + offset, "%2s%n ", c, &nrchars); |
| 229 | if (ret != 1) |
| 230 | goto err; |
| 231 | |
| 232 | ret = sscanf(c, "%2x", &cmd); |
| 233 | if (ret != 1) |
| 234 | goto err; |
| 235 | |
| 236 | pattern[i] = (u8)cmd; |
| 237 | offset += nrchars; |
| 238 | i++; |
| 239 | } |
| 240 | |
| 241 | /* Each instruction is 16bit long. Check that length is even */ |
| 242 | if (i % 2) |
| 243 | goto err; |
| 244 | |
| 245 | program_size = i; |
| 246 | for (i = 0; i < program_size; i++) |
| 247 | lp55xx_write(chip, addr[idx] + i, pattern[i]); |
| 248 | |
| 249 | return 0; |
| 250 | |
| 251 | err: |
| 252 | dev_err(&chip->cl->dev, "wrong pattern format\n"); |
| 253 | return -EINVAL; |
| 254 | } |
| 255 | |
| 256 | static void lp5521_firmware_loaded(struct lp55xx_chip *chip) |
| 257 | { |
| 258 | const struct firmware *fw = chip->fw; |
| 259 | |
| 260 | if (fw->size > LP5521_PROGRAM_LENGTH) { |
| 261 | dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n", |
| 262 | fw->size); |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * Program momery sequence |
| 268 | * 1) set engine mode to "LOAD" |
| 269 | * 2) write firmware data into program memory |
| 270 | */ |
| 271 | |
| 272 | lp5521_load_engine(chip); |
| 273 | lp5521_update_program_memory(chip, fw->data, fw->size); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 276 | static int lp5521_post_init_device(struct lp55xx_chip *chip) |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 277 | { |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 278 | int ret; |
Milo(Woogyom) Kim | 9448217 | 2013-02-05 18:03:55 +0900 | [diff] [blame] | 279 | u8 val; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 280 | |
Milo(Woogyom) Kim | 9448217 | 2013-02-05 18:03:55 +0900 | [diff] [blame] | 281 | /* |
| 282 | * Make sure that the chip is reset by reading back the r channel |
| 283 | * current reg. This is dummy read is required on some platforms - |
| 284 | * otherwise further access to the R G B channels in the |
| 285 | * LP5521_REG_ENABLE register will not have any effect - strange! |
| 286 | */ |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 287 | ret = lp55xx_read(chip, LP5521_REG_R_CURRENT, &val); |
Milo(Woogyom) Kim | 9448217 | 2013-02-05 18:03:55 +0900 | [diff] [blame] | 288 | if (ret) { |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 289 | dev_err(&chip->cl->dev, "error in resetting chip\n"); |
Milo(Woogyom) Kim | 9448217 | 2013-02-05 18:03:55 +0900 | [diff] [blame] | 290 | return ret; |
| 291 | } |
| 292 | if (val != LP5521_REG_R_CURR_DEFAULT) { |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 293 | dev_err(&chip->cl->dev, |
Milo(Woogyom) Kim | 9448217 | 2013-02-05 18:03:55 +0900 | [diff] [blame] | 294 | "unexpected data in register (expected 0x%x got 0x%x)\n", |
| 295 | LP5521_REG_R_CURR_DEFAULT, val); |
| 296 | ret = -EINVAL; |
| 297 | return ret; |
| 298 | } |
| 299 | usleep_range(10000, 20000); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 300 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 301 | /* Set all PWMs to direct control mode */ |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 302 | ret = lp55xx_write(chip, LP5521_REG_OP_MODE, LP5521_CMD_DIRECT); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 303 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 304 | val = chip->pdata->update_config ? |
Kim, Milo | 3b49aac | 2012-03-23 15:02:08 -0700 | [diff] [blame] | 305 | : (LP5521_PWRSAVE_EN | LP5521_CP_MODE_AUTO | LP5521_R_TO_BATT); |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 306 | ret = lp55xx_write(chip, LP5521_REG_CONFIG, val); |
Milo(Woogyom) Kim | 9448217 | 2013-02-05 18:03:55 +0900 | [diff] [blame] | 307 | if (ret) |
| 308 | return ret; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 309 | |
| 310 | /* Initialize all channels PWM to zero -> leds off */ |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 311 | lp55xx_write(chip, LP5521_REG_R_PWM, 0); |
| 312 | lp55xx_write(chip, LP5521_REG_G_PWM, 0); |
| 313 | lp55xx_write(chip, LP5521_REG_B_PWM, 0); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 314 | |
| 315 | /* Set engines are set to run state when OP_MODE enables engines */ |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 316 | ret = lp55xx_write(chip, LP5521_REG_ENABLE, LP5521_ENABLE_RUN_PROGRAM); |
Milo(Woogyom) Kim | 9448217 | 2013-02-05 18:03:55 +0900 | [diff] [blame] | 317 | if (ret) |
| 318 | return ret; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 319 | |
Milo(Woogyom) Kim | 9448217 | 2013-02-05 18:03:55 +0900 | [diff] [blame] | 320 | lp5521_wait_enable_done(); |
| 321 | |
| 322 | return 0; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 325 | static int lp5521_run_selftest(struct lp55xx_chip *chip, char *buf) |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 326 | { |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 327 | struct lp55xx_platform_data *pdata = chip->pdata; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 328 | int ret; |
| 329 | u8 status; |
| 330 | |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 331 | ret = lp55xx_read(chip, LP5521_REG_STATUS, &status); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 332 | if (ret < 0) |
| 333 | return ret; |
| 334 | |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 335 | if (pdata->clock_mode != LP55XX_CLOCK_EXT) |
| 336 | return 0; |
| 337 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 338 | /* Check that ext clock is really in use if requested */ |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 339 | if ((status & LP5521_EXT_CLK_USED) == 0) |
| 340 | return -EIO; |
| 341 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 345 | static void lp5521_led_brightness_work(struct work_struct *work) |
| 346 | { |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame] | 347 | struct lp55xx_led *led = container_of(work, struct lp55xx_led, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 348 | brightness_work); |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame] | 349 | struct lp55xx_chip *chip = led->chip; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 350 | |
| 351 | mutex_lock(&chip->lock); |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame] | 352 | lp55xx_write(chip, LP5521_REG_LED_PWM_BASE + led->chan_nr, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 353 | led->brightness); |
| 354 | mutex_unlock(&chip->lock); |
| 355 | } |
| 356 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 357 | static ssize_t lp5521_selftest(struct device *dev, |
| 358 | struct device_attribute *attr, |
| 359 | char *buf) |
| 360 | { |
Milo(Woogyom) Kim | 9ca3bd8 | 2013-02-05 19:21:43 +0900 | [diff] [blame] | 361 | struct lp55xx_led *led = i2c_get_clientdata(to_i2c_client(dev)); |
| 362 | struct lp55xx_chip *chip = led->chip; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 363 | int ret; |
| 364 | |
| 365 | mutex_lock(&chip->lock); |
| 366 | ret = lp5521_run_selftest(chip, buf); |
| 367 | mutex_unlock(&chip->lock); |
| 368 | return sprintf(buf, "%s\n", ret ? "FAIL" : "OK"); |
| 369 | } |
| 370 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 371 | /* device attributes */ |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 372 | static DEVICE_ATTR(selftest, S_IRUGO, lp5521_selftest, NULL); |
| 373 | |
| 374 | static struct attribute *lp5521_attributes[] = { |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 375 | &dev_attr_selftest.attr, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 376 | NULL |
| 377 | }; |
| 378 | |
| 379 | static const struct attribute_group lp5521_group = { |
| 380 | .attrs = lp5521_attributes, |
| 381 | }; |
| 382 | |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 383 | /* Chip specific configurations */ |
| 384 | static struct lp55xx_device_config lp5521_cfg = { |
| 385 | .reset = { |
| 386 | .addr = LP5521_REG_RESET, |
| 387 | .val = LP5521_RESET, |
| 388 | }, |
Milo(Woogyom) Kim | e3a700d | 2013-02-05 18:09:56 +0900 | [diff] [blame] | 389 | .enable = { |
| 390 | .addr = LP5521_REG_ENABLE, |
| 391 | .val = LP5521_ENABLE_DEFAULT, |
| 392 | }, |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 393 | .max_channel = LP5521_MAX_LEDS, |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 394 | .post_init_device = lp5521_post_init_device, |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame] | 395 | .brightness_work_fn = lp5521_led_brightness_work, |
Milo(Woogyom) Kim | a96bfa1 | 2013-02-05 19:09:32 +0900 | [diff] [blame] | 396 | .set_led_current = lp5521_set_led_current, |
Milo(Woogyom) Kim | 9ce7cb1 | 2013-02-05 19:18:10 +0900 | [diff] [blame] | 397 | .firmware_cb = lp5521_firmware_loaded, |
| 398 | .run_engine = lp5521_run_engine, |
Milo(Woogyom) Kim | e73c0ce | 2013-02-05 19:20:45 +0900 | [diff] [blame] | 399 | .dev_attr_group = &lp5521_group, |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 400 | }; |
| 401 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 402 | static int lp5521_probe(struct i2c_client *client, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 403 | const struct i2c_device_id *id) |
| 404 | { |
Milo(Woogyom) Kim | 1904f83 | 2013-02-05 17:56:23 +0900 | [diff] [blame] | 405 | int ret; |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 406 | struct lp55xx_chip *chip; |
| 407 | struct lp55xx_led *led; |
| 408 | struct lp55xx_platform_data *pdata = client->dev.platform_data; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 409 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 410 | if (!pdata) { |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 411 | dev_err(&client->dev, "no platform data\n"); |
Bryan Wu | e430dc0 | 2012-07-04 11:16:09 +0800 | [diff] [blame] | 412 | return -EINVAL; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 413 | } |
| 414 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 415 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); |
| 416 | if (!chip) |
| 417 | return -ENOMEM; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 418 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 419 | led = devm_kzalloc(&client->dev, |
| 420 | sizeof(*led) * pdata->num_channels, GFP_KERNEL); |
| 421 | if (!led) |
| 422 | return -ENOMEM; |
| 423 | |
| 424 | chip->cl = client; |
| 425 | chip->pdata = pdata; |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 426 | chip->cfg = &lp5521_cfg; |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 427 | |
| 428 | mutex_init(&chip->lock); |
| 429 | |
| 430 | i2c_set_clientdata(client, led); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 431 | |
Milo(Woogyom) Kim | 22ebeb4 | 2013-02-05 18:58:35 +0900 | [diff] [blame] | 432 | ret = lp55xx_init_device(chip); |
Milo(Woogyom) Kim | 944f7b1 | 2013-02-05 17:49:46 +0900 | [diff] [blame] | 433 | if (ret) |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 434 | goto err_init; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 435 | |
| 436 | dev_info(&client->dev, "%s programmable led chip found\n", id->name); |
| 437 | |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 438 | ret = lp55xx_register_leds(led, chip); |
Milo(Woogyom) Kim | f652480 | 2013-02-05 17:53:40 +0900 | [diff] [blame] | 439 | if (ret) |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 440 | goto err_register_leds; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 441 | |
Milo(Woogyom) Kim | e73c0ce | 2013-02-05 19:20:45 +0900 | [diff] [blame] | 442 | ret = lp55xx_register_sysfs(chip); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 443 | if (ret) { |
| 444 | dev_err(&client->dev, "registering sysfs failed\n"); |
Milo(Woogyom) Kim | e73c0ce | 2013-02-05 19:20:45 +0900 | [diff] [blame] | 445 | goto err_register_sysfs; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 446 | } |
Milo(Woogyom) Kim | e73c0ce | 2013-02-05 19:20:45 +0900 | [diff] [blame] | 447 | |
| 448 | return 0; |
| 449 | |
| 450 | err_register_sysfs: |
Milo(Woogyom) Kim | c3a68eb | 2013-02-05 19:11:18 +0900 | [diff] [blame] | 451 | lp55xx_unregister_leds(led, chip); |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 452 | err_register_leds: |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 453 | lp55xx_deinit_device(chip); |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 454 | err_init: |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 455 | return ret; |
| 456 | } |
| 457 | |
Bill Pemberton | 678e8a6 | 2012-11-19 13:26:00 -0500 | [diff] [blame] | 458 | static int lp5521_remove(struct i2c_client *client) |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 459 | { |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 460 | struct lp55xx_led *led = i2c_get_clientdata(client); |
| 461 | struct lp55xx_chip *chip = led->chip; |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 462 | |
Milo(Woogyom) Kim | 87cc4bd | 2013-02-05 19:23:51 +0900 | [diff] [blame] | 463 | lp5521_stop_engine(chip); |
| 464 | lp55xx_unregister_sysfs(chip); |
Milo(Woogyom) Kim | c3a68eb | 2013-02-05 19:11:18 +0900 | [diff] [blame] | 465 | lp55xx_unregister_leds(led, chip); |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 466 | lp55xx_deinit_device(chip); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 467 | |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static const struct i2c_device_id lp5521_id[] = { |
| 472 | { "lp5521", 0 }, /* Three channel chip */ |
| 473 | { } |
| 474 | }; |
| 475 | MODULE_DEVICE_TABLE(i2c, lp5521_id); |
| 476 | |
| 477 | static struct i2c_driver lp5521_driver = { |
| 478 | .driver = { |
| 479 | .name = "lp5521", |
| 480 | }, |
| 481 | .probe = lp5521_probe, |
Bill Pemberton | df07cf8 | 2012-11-19 13:20:20 -0500 | [diff] [blame] | 482 | .remove = lp5521_remove, |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 483 | .id_table = lp5521_id, |
| 484 | }; |
| 485 | |
Axel Lin | 09a0d18 | 2012-01-10 15:09:27 -0800 | [diff] [blame] | 486 | module_i2c_driver(lp5521_driver); |
Samu Onkalo | 500fe14 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 487 | |
| 488 | MODULE_AUTHOR("Mathias Nyman, Yuri Zaporozhets, Samu Onkalo"); |
| 489 | MODULE_DESCRIPTION("LP5521 LED engine"); |
| 490 | MODULE_LICENSE("GPL v2"); |