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 | |
| 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-lp5523.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 | db6eaf8 | 2013-02-05 19:18:51 +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 | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 41 | |
| 42 | #define LP5523_REG_ENABLE 0x00 |
| 43 | #define LP5523_REG_OP_MODE 0x01 |
| 44 | #define LP5523_REG_RATIOMETRIC_MSB 0x02 |
| 45 | #define LP5523_REG_RATIOMETRIC_LSB 0x03 |
| 46 | #define LP5523_REG_ENABLE_LEDS_MSB 0x04 |
| 47 | #define LP5523_REG_ENABLE_LEDS_LSB 0x05 |
| 48 | #define LP5523_REG_LED_CNTRL_BASE 0x06 |
| 49 | #define LP5523_REG_LED_PWM_BASE 0x16 |
| 50 | #define LP5523_REG_LED_CURRENT_BASE 0x26 |
| 51 | #define LP5523_REG_CONFIG 0x36 |
| 52 | #define LP5523_REG_CHANNEL1_PC 0x37 |
| 53 | #define LP5523_REG_CHANNEL2_PC 0x38 |
| 54 | #define LP5523_REG_CHANNEL3_PC 0x39 |
| 55 | #define LP5523_REG_STATUS 0x3a |
| 56 | #define LP5523_REG_GPO 0x3b |
| 57 | #define LP5523_REG_VARIABLE 0x3c |
| 58 | #define LP5523_REG_RESET 0x3d |
| 59 | #define LP5523_REG_TEMP_CTRL 0x3e |
| 60 | #define LP5523_REG_TEMP_READ 0x3f |
| 61 | #define LP5523_REG_TEMP_WRITE 0x40 |
| 62 | #define LP5523_REG_LED_TEST_CTRL 0x41 |
| 63 | #define LP5523_REG_LED_TEST_ADC 0x42 |
| 64 | #define LP5523_REG_ENG1_VARIABLE 0x45 |
| 65 | #define LP5523_REG_ENG2_VARIABLE 0x46 |
| 66 | #define LP5523_REG_ENG3_VARIABLE 0x47 |
| 67 | #define LP5523_REG_MASTER_FADER1 0x48 |
| 68 | #define LP5523_REG_MASTER_FADER2 0x49 |
| 69 | #define LP5523_REG_MASTER_FADER3 0x4a |
| 70 | #define LP5523_REG_CH1_PROG_START 0x4c |
| 71 | #define LP5523_REG_CH2_PROG_START 0x4d |
| 72 | #define LP5523_REG_CH3_PROG_START 0x4e |
| 73 | #define LP5523_REG_PROG_PAGE_SEL 0x4f |
| 74 | #define LP5523_REG_PROG_MEM 0x50 |
| 75 | |
| 76 | #define LP5523_CMD_LOAD 0x15 /* 00010101 */ |
| 77 | #define LP5523_CMD_RUN 0x2a /* 00101010 */ |
| 78 | #define LP5523_CMD_DISABLED 0x00 /* 00000000 */ |
| 79 | |
| 80 | #define LP5523_ENABLE 0x40 |
| 81 | #define LP5523_AUTO_INC 0x40 |
| 82 | #define LP5523_PWR_SAVE 0x20 |
| 83 | #define LP5523_PWM_PWR_SAVE 0x04 |
| 84 | #define LP5523_CP_1 0x08 |
| 85 | #define LP5523_CP_1_5 0x10 |
| 86 | #define LP5523_CP_AUTO 0x18 |
| 87 | #define LP5523_INT_CLK 0x01 |
| 88 | #define LP5523_AUTO_CLK 0x02 |
| 89 | #define LP5523_EN_LEDTEST 0x80 |
| 90 | #define LP5523_LEDTEST_DONE 0x80 |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 91 | #define LP5523_RESET 0xFF |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 92 | |
| 93 | #define LP5523_DEFAULT_CURRENT 50 /* microAmps */ |
| 94 | #define LP5523_PROGRAM_LENGTH 32 /* in bytes */ |
| 95 | #define LP5523_PROGRAM_PAGES 6 |
| 96 | #define LP5523_ADC_SHORTCIRC_LIM 80 |
| 97 | |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 98 | #define LP5523_MAX_LEDS 9 |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 99 | #define LP5523_ENGINES 3 |
| 100 | |
| 101 | #define LP5523_ENG_MASK_BASE 0x30 /* 00110000 */ |
| 102 | |
| 103 | #define LP5523_ENG_STATUS_MASK 0x07 /* 00000111 */ |
| 104 | |
| 105 | #define LP5523_IRQ_FLAGS IRQF_TRIGGER_FALLING |
| 106 | |
| 107 | #define LP5523_EXT_CLK_USED 0x08 |
| 108 | |
| 109 | #define LED_ACTIVE(mux, led) (!!(mux & (0x0001 << led))) |
| 110 | #define SHIFT_MASK(id) (((id) - 1) * 2) |
| 111 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 112 | /* Memory Page Selection */ |
| 113 | #define LP5523_PAGE_ENG1 0 |
| 114 | #define LP5523_PAGE_ENG2 1 |
| 115 | #define LP5523_PAGE_ENG3 2 |
| 116 | |
| 117 | /* Program Memory Operations */ |
| 118 | #define LP5523_MODE_ENG1_M 0x30 /* Operation Mode Register */ |
| 119 | #define LP5523_MODE_ENG2_M 0x0C |
| 120 | #define LP5523_MODE_ENG3_M 0x03 |
| 121 | #define LP5523_LOAD_ENG1 0x10 |
| 122 | #define LP5523_LOAD_ENG2 0x04 |
| 123 | #define LP5523_LOAD_ENG3 0x01 |
| 124 | |
| 125 | #define LP5523_ENG1_IS_LOADING(mode) \ |
| 126 | ((mode & LP5523_MODE_ENG1_M) == LP5523_LOAD_ENG1) |
| 127 | #define LP5523_ENG2_IS_LOADING(mode) \ |
| 128 | ((mode & LP5523_MODE_ENG2_M) == LP5523_LOAD_ENG2) |
| 129 | #define LP5523_ENG3_IS_LOADING(mode) \ |
| 130 | ((mode & LP5523_MODE_ENG3_M) == LP5523_LOAD_ENG3) |
| 131 | |
| 132 | #define LP5523_EXEC_ENG1_M 0x30 /* Enable Register */ |
| 133 | #define LP5523_EXEC_ENG2_M 0x0C |
| 134 | #define LP5523_EXEC_ENG3_M 0x03 |
| 135 | #define LP5523_EXEC_M 0x3F |
| 136 | #define LP5523_RUN_ENG1 0x20 |
| 137 | #define LP5523_RUN_ENG2 0x08 |
| 138 | #define LP5523_RUN_ENG3 0x02 |
| 139 | |
Kim, Milo | 27d7704 | 2012-09-04 15:06:18 +0800 | [diff] [blame] | 140 | enum lp5523_chip_id { |
| 141 | LP5523, |
| 142 | LP55231, |
| 143 | }; |
| 144 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 145 | struct lp5523_led { |
| 146 | int id; |
| 147 | u8 chan_nr; |
| 148 | u8 led_current; |
| 149 | u8 max_current; |
| 150 | struct led_classdev cdev; |
| 151 | struct work_struct brightness_work; |
| 152 | u8 brightness; |
| 153 | }; |
| 154 | |
| 155 | struct lp5523_chip { |
| 156 | struct mutex lock; /* Serialize control */ |
| 157 | struct i2c_client *client; |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 158 | struct lp5523_led leds[LP5523_MAX_LEDS]; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 159 | struct lp5523_platform_data *pdata; |
| 160 | u8 num_channels; |
| 161 | u8 num_leds; |
| 162 | }; |
| 163 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 164 | static inline void lp5523_wait_opmode_done(void) |
| 165 | { |
| 166 | usleep_range(1000, 2000); |
| 167 | } |
| 168 | |
Milo(Woogyom) Kim | a96bfa1 | 2013-02-05 19:09:32 +0900 | [diff] [blame] | 169 | static void lp5523_set_led_current(struct lp55xx_led *led, u8 led_current) |
| 170 | { |
| 171 | led->led_current = led_current; |
| 172 | lp55xx_write(led->chip, LP5523_REG_LED_CURRENT_BASE + led->chan_nr, |
| 173 | led_current); |
| 174 | } |
| 175 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 176 | static int lp5523_write(struct i2c_client *client, u8 reg, u8 value) |
| 177 | { |
| 178 | return i2c_smbus_write_byte_data(client, reg, value); |
| 179 | } |
| 180 | |
| 181 | static int lp5523_read(struct i2c_client *client, u8 reg, u8 *buf) |
| 182 | { |
| 183 | s32 ret = i2c_smbus_read_byte_data(client, reg); |
| 184 | |
| 185 | if (ret < 0) |
Sachin Kamat | 5ebab74 | 2012-11-20 01:59:01 -0800 | [diff] [blame] | 186 | return ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 187 | |
| 188 | *buf = ret; |
| 189 | return 0; |
| 190 | } |
| 191 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 192 | static int lp5523_post_init_device(struct lp55xx_chip *chip) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 193 | { |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 194 | int ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 195 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 196 | ret = lp55xx_write(chip, LP5523_REG_ENABLE, LP5523_ENABLE); |
Milo(Woogyom) Kim | 632418b | 2013-02-05 18:04:50 +0900 | [diff] [blame] | 197 | if (ret) |
| 198 | return ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 199 | |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 200 | /* Chip startup time is 500 us, 1 - 2 ms gives some margin */ |
| 201 | usleep_range(1000, 2000); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 202 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 203 | ret = lp55xx_write(chip, LP5523_REG_CONFIG, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 204 | LP5523_AUTO_INC | LP5523_PWR_SAVE | |
| 205 | LP5523_CP_AUTO | LP5523_AUTO_CLK | |
| 206 | LP5523_PWM_PWR_SAVE); |
Milo(Woogyom) Kim | 632418b | 2013-02-05 18:04:50 +0900 | [diff] [blame] | 207 | if (ret) |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 208 | return ret; |
| 209 | |
Milo(Woogyom) Kim | 632418b | 2013-02-05 18:04:50 +0900 | [diff] [blame] | 210 | /* turn on all leds */ |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 211 | ret = lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_MSB, 0x01); |
Milo(Woogyom) Kim | 632418b | 2013-02-05 18:04:50 +0900 | [diff] [blame] | 212 | if (ret) |
| 213 | return ret; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 214 | |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 215 | return lp55xx_write(chip, LP5523_REG_ENABLE_LEDS_LSB, 0xff); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 218 | static void lp5523_load_engine(struct lp55xx_chip *chip) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 219 | { |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 220 | enum lp55xx_engine_index idx = chip->engine_idx; |
| 221 | u8 mask[] = { |
| 222 | [LP55XX_ENGINE_1] = LP5523_MODE_ENG1_M, |
| 223 | [LP55XX_ENGINE_2] = LP5523_MODE_ENG2_M, |
| 224 | [LP55XX_ENGINE_3] = LP5523_MODE_ENG3_M, |
| 225 | }; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 226 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 227 | u8 val[] = { |
| 228 | [LP55XX_ENGINE_1] = LP5523_LOAD_ENG1, |
| 229 | [LP55XX_ENGINE_2] = LP5523_LOAD_ENG2, |
| 230 | [LP55XX_ENGINE_3] = LP5523_LOAD_ENG3, |
| 231 | }; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 232 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 233 | u8 page_sel[] = { |
| 234 | [LP55XX_ENGINE_1] = LP5523_PAGE_ENG1, |
| 235 | [LP55XX_ENGINE_2] = LP5523_PAGE_ENG2, |
| 236 | [LP55XX_ENGINE_3] = LP5523_PAGE_ENG3, |
| 237 | }; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 238 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 239 | lp55xx_update_bits(chip, LP5523_REG_OP_MODE, mask[idx], val[idx]); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 240 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 241 | lp5523_wait_opmode_done(); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 242 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 243 | lp55xx_write(chip, LP5523_REG_PROG_PAGE_SEL, page_sel[idx]); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 246 | static void lp5523_stop_engine(struct lp55xx_chip *chip) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 247 | { |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 248 | lp55xx_write(chip, LP5523_REG_OP_MODE, 0); |
| 249 | lp5523_wait_opmode_done(); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 252 | static void lp5523_turn_off_channels(struct lp55xx_chip *chip) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 253 | { |
| 254 | int i; |
Kim, Milo | 469eba0 | 2012-08-21 17:04:02 +0800 | [diff] [blame] | 255 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 256 | for (i = 0; i < LP5523_MAX_LEDS; i++) |
| 257 | lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + i, 0); |
| 258 | } |
| 259 | |
| 260 | static void lp5523_run_engine(struct lp55xx_chip *chip, bool start) |
| 261 | { |
| 262 | int ret; |
| 263 | u8 mode; |
| 264 | u8 exec; |
| 265 | |
| 266 | /* stop engine */ |
| 267 | if (!start) { |
| 268 | lp5523_stop_engine(chip); |
| 269 | lp5523_turn_off_channels(chip); |
| 270 | return; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 271 | } |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 272 | |
| 273 | /* |
| 274 | * To run the engine, |
| 275 | * operation mode and enable register should updated at the same time |
| 276 | */ |
| 277 | |
| 278 | ret = lp55xx_read(chip, LP5523_REG_OP_MODE, &mode); |
| 279 | if (ret) |
| 280 | return; |
| 281 | |
| 282 | ret = lp55xx_read(chip, LP5523_REG_ENABLE, &exec); |
| 283 | if (ret) |
| 284 | return; |
| 285 | |
| 286 | /* change operation mode to RUN only when each engine is loading */ |
| 287 | if (LP5523_ENG1_IS_LOADING(mode)) { |
| 288 | mode = (mode & ~LP5523_MODE_ENG1_M) | LP5523_RUN_ENG1; |
| 289 | exec = (exec & ~LP5523_EXEC_ENG1_M) | LP5523_RUN_ENG1; |
| 290 | } |
| 291 | |
| 292 | if (LP5523_ENG2_IS_LOADING(mode)) { |
| 293 | mode = (mode & ~LP5523_MODE_ENG2_M) | LP5523_RUN_ENG2; |
| 294 | exec = (exec & ~LP5523_EXEC_ENG2_M) | LP5523_RUN_ENG2; |
| 295 | } |
| 296 | |
| 297 | if (LP5523_ENG3_IS_LOADING(mode)) { |
| 298 | mode = (mode & ~LP5523_MODE_ENG3_M) | LP5523_RUN_ENG3; |
| 299 | exec = (exec & ~LP5523_EXEC_ENG3_M) | LP5523_RUN_ENG3; |
| 300 | } |
| 301 | |
| 302 | lp55xx_write(chip, LP5523_REG_OP_MODE, mode); |
| 303 | lp5523_wait_opmode_done(); |
| 304 | |
| 305 | lp55xx_update_bits(chip, LP5523_REG_ENABLE, LP5523_EXEC_M, exec); |
| 306 | } |
| 307 | |
| 308 | static int lp5523_update_program_memory(struct lp55xx_chip *chip, |
| 309 | const u8 *data, size_t size) |
| 310 | { |
| 311 | u8 pattern[LP5523_PROGRAM_LENGTH] = {0}; |
| 312 | unsigned cmd; |
| 313 | char c[3]; |
| 314 | int update_size; |
| 315 | int nrchars; |
| 316 | int offset = 0; |
| 317 | int ret; |
| 318 | int i; |
| 319 | |
| 320 | /* clear program memory before updating */ |
| 321 | for (i = 0; i < LP5523_PROGRAM_LENGTH; i++) |
| 322 | lp55xx_write(chip, LP5523_REG_PROG_MEM + i, 0); |
| 323 | |
| 324 | i = 0; |
| 325 | while ((offset < size - 1) && (i < LP5523_PROGRAM_LENGTH)) { |
| 326 | /* separate sscanfs because length is working only for %s */ |
| 327 | ret = sscanf(data + offset, "%2s%n ", c, &nrchars); |
| 328 | if (ret != 1) |
| 329 | goto err; |
| 330 | |
| 331 | ret = sscanf(c, "%2x", &cmd); |
| 332 | if (ret != 1) |
| 333 | goto err; |
| 334 | |
| 335 | pattern[i] = (u8)cmd; |
| 336 | offset += nrchars; |
| 337 | i++; |
| 338 | } |
| 339 | |
| 340 | /* Each instruction is 16bit long. Check that length is even */ |
| 341 | if (i % 2) |
| 342 | goto err; |
| 343 | |
| 344 | update_size = i; |
| 345 | for (i = 0; i < update_size; i++) |
| 346 | lp55xx_write(chip, LP5523_REG_PROG_MEM + i, pattern[i]); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 347 | |
| 348 | return 0; |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 349 | |
| 350 | err: |
| 351 | dev_err(&chip->cl->dev, "wrong pattern format\n"); |
| 352 | return -EINVAL; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 353 | } |
| 354 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 355 | static void lp5523_firmware_loaded(struct lp55xx_chip *chip) |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 356 | { |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 357 | const struct firmware *fw = chip->fw; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 358 | |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 359 | if (fw->size > LP5523_PROGRAM_LENGTH) { |
| 360 | dev_err(&chip->cl->dev, "firmware data size overflow: %zu\n", |
| 361 | fw->size); |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Program momery sequence |
| 367 | * 1) set engine mode to "LOAD" |
| 368 | * 2) write firmware data into program memory |
| 369 | */ |
| 370 | |
| 371 | lp5523_load_engine(chip); |
| 372 | lp5523_update_program_memory(chip, fw->data, fw->size); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 375 | static ssize_t lp5523_selftest(struct device *dev, |
| 376 | struct device_attribute *attr, |
| 377 | char *buf) |
| 378 | { |
| 379 | struct i2c_client *client = to_i2c_client(dev); |
| 380 | struct lp5523_chip *chip = i2c_get_clientdata(client); |
| 381 | int i, ret, pos = 0; |
| 382 | int led = 0; |
| 383 | u8 status, adc, vdd; |
| 384 | |
| 385 | mutex_lock(&chip->lock); |
| 386 | |
| 387 | ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status); |
| 388 | if (ret < 0) |
| 389 | goto fail; |
| 390 | |
| 391 | /* Check that ext clock is really in use if requested */ |
| 392 | if ((chip->pdata) && (chip->pdata->clock_mode == LP5523_CLOCK_EXT)) |
| 393 | if ((status & LP5523_EXT_CLK_USED) == 0) |
| 394 | goto fail; |
| 395 | |
| 396 | /* Measure VDD (i.e. VBAT) first (channel 16 corresponds to VDD) */ |
| 397 | lp5523_write(chip->client, LP5523_REG_LED_TEST_CTRL, |
| 398 | LP5523_EN_LEDTEST | 16); |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 399 | usleep_range(3000, 6000); /* ADC conversion time is typically 2.7 ms */ |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 400 | ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status); |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 401 | if (ret < 0) |
| 402 | goto fail; |
| 403 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 404 | if (!(status & LP5523_LEDTEST_DONE)) |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 405 | usleep_range(3000, 6000); /* Was not ready. Wait little bit */ |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 406 | |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 407 | ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &vdd); |
| 408 | if (ret < 0) |
| 409 | goto fail; |
| 410 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 411 | vdd--; /* There may be some fluctuation in measurement */ |
| 412 | |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 413 | for (i = 0; i < LP5523_MAX_LEDS; i++) { |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 414 | /* Skip non-existing channels */ |
| 415 | if (chip->pdata->led_config[i].led_current == 0) |
| 416 | continue; |
| 417 | |
| 418 | /* Set default current */ |
| 419 | lp5523_write(chip->client, |
| 420 | LP5523_REG_LED_CURRENT_BASE + i, |
| 421 | chip->pdata->led_config[i].led_current); |
| 422 | |
| 423 | lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0xff); |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 424 | /* let current stabilize 2 - 4ms before measurements start */ |
| 425 | usleep_range(2000, 4000); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 426 | lp5523_write(chip->client, |
| 427 | LP5523_REG_LED_TEST_CTRL, |
| 428 | LP5523_EN_LEDTEST | i); |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 429 | /* ADC conversion time is 2.7 ms typically */ |
| 430 | usleep_range(3000, 6000); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 431 | ret = lp5523_read(chip->client, LP5523_REG_STATUS, &status); |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 432 | if (ret < 0) |
| 433 | goto fail; |
| 434 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 435 | if (!(status & LP5523_LEDTEST_DONE)) |
Samu Onkalo | 2e4840e | 2010-11-24 12:57:04 -0800 | [diff] [blame] | 436 | usleep_range(3000, 6000);/* Was not ready. Wait. */ |
Jingoo Han | 1b21ec5 | 2012-10-28 18:45:11 -0700 | [diff] [blame] | 437 | ret = lp5523_read(chip->client, LP5523_REG_LED_TEST_ADC, &adc); |
| 438 | if (ret < 0) |
| 439 | goto fail; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 440 | |
| 441 | if (adc >= vdd || adc < LP5523_ADC_SHORTCIRC_LIM) |
| 442 | pos += sprintf(buf + pos, "LED %d FAIL\n", i); |
| 443 | |
| 444 | lp5523_write(chip->client, LP5523_REG_LED_PWM_BASE + i, 0x00); |
| 445 | |
| 446 | /* Restore current */ |
| 447 | lp5523_write(chip->client, |
| 448 | LP5523_REG_LED_CURRENT_BASE + i, |
| 449 | chip->leds[led].led_current); |
| 450 | led++; |
| 451 | } |
| 452 | if (pos == 0) |
| 453 | pos = sprintf(buf, "OK\n"); |
| 454 | goto release_lock; |
| 455 | fail: |
| 456 | pos = sprintf(buf, "FAIL\n"); |
| 457 | |
| 458 | release_lock: |
| 459 | mutex_unlock(&chip->lock); |
| 460 | |
| 461 | return pos; |
| 462 | } |
| 463 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 464 | static void lp5523_led_brightness_work(struct work_struct *work) |
| 465 | { |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame] | 466 | struct lp55xx_led *led = container_of(work, struct lp55xx_led, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 467 | brightness_work); |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame] | 468 | struct lp55xx_chip *chip = led->chip; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 469 | |
| 470 | mutex_lock(&chip->lock); |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame] | 471 | lp55xx_write(chip, LP5523_REG_LED_PWM_BASE + led->chan_nr, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 472 | led->brightness); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 473 | mutex_unlock(&chip->lock); |
| 474 | } |
| 475 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 476 | static DEVICE_ATTR(selftest, S_IRUGO, lp5523_selftest, NULL); |
| 477 | |
| 478 | static struct attribute *lp5523_attributes[] = { |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 479 | &dev_attr_selftest.attr, |
Kim, Milo | f162584 | 2012-09-12 20:16:03 +0800 | [diff] [blame] | 480 | NULL, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 481 | }; |
| 482 | |
| 483 | static const struct attribute_group lp5523_group = { |
| 484 | .attrs = lp5523_attributes, |
| 485 | }; |
| 486 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 487 | static int lp5523_register_sysfs(struct i2c_client *client) |
| 488 | { |
| 489 | struct device *dev = &client->dev; |
| 490 | int ret; |
| 491 | |
| 492 | ret = sysfs_create_group(&dev->kobj, &lp5523_group); |
| 493 | if (ret < 0) |
| 494 | return ret; |
| 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | static void lp5523_unregister_sysfs(struct i2c_client *client) |
| 500 | { |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 501 | struct device *dev = &client->dev; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 502 | |
| 503 | sysfs_remove_group(&dev->kobj, &lp5523_group); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 504 | } |
| 505 | |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 506 | /* Chip specific configurations */ |
| 507 | static struct lp55xx_device_config lp5523_cfg = { |
| 508 | .reset = { |
| 509 | .addr = LP5523_REG_RESET, |
| 510 | .val = LP5523_RESET, |
| 511 | }, |
Milo(Woogyom) Kim | e3a700d | 2013-02-05 18:09:56 +0900 | [diff] [blame] | 512 | .enable = { |
| 513 | .addr = LP5523_REG_ENABLE, |
| 514 | .val = LP5523_ENABLE, |
| 515 | }, |
Milo(Woogyom) Kim | 0e20234 | 2013-02-05 19:07:34 +0900 | [diff] [blame] | 516 | .max_channel = LP5523_MAX_LEDS, |
Milo(Woogyom) Kim | ffbdccd | 2013-02-05 18:57:36 +0900 | [diff] [blame] | 517 | .post_init_device = lp5523_post_init_device, |
Milo(Woogyom) Kim | a6e4679 | 2013-02-05 19:08:40 +0900 | [diff] [blame] | 518 | .brightness_work_fn = lp5523_led_brightness_work, |
Milo(Woogyom) Kim | a96bfa1 | 2013-02-05 19:09:32 +0900 | [diff] [blame] | 519 | .set_led_current = lp5523_set_led_current, |
Milo(Woogyom) Kim | db6eaf8 | 2013-02-05 19:18:51 +0900 | [diff] [blame^] | 520 | .firmware_cb = lp5523_firmware_loaded, |
| 521 | .run_engine = lp5523_run_engine, |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 522 | }; |
| 523 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 524 | static int lp5523_probe(struct i2c_client *client, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 525 | const struct i2c_device_id *id) |
| 526 | { |
Milo(Woogyom) Kim | 22ebeb4 | 2013-02-05 18:58:35 +0900 | [diff] [blame] | 527 | int ret; |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 528 | struct lp55xx_chip *chip; |
| 529 | struct lp55xx_led *led; |
| 530 | struct lp55xx_platform_data *pdata = client->dev.platform_data; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 531 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 532 | if (!pdata) { |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 533 | dev_err(&client->dev, "no platform data\n"); |
Bryan Wu | 94ca4bc | 2012-07-04 11:44:18 +0800 | [diff] [blame] | 534 | return -EINVAL; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 535 | } |
| 536 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 537 | chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL); |
| 538 | if (!chip) |
| 539 | return -ENOMEM; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 540 | |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 541 | led = devm_kzalloc(&client->dev, |
| 542 | sizeof(*led) * pdata->num_channels, GFP_KERNEL); |
| 543 | if (!led) |
| 544 | return -ENOMEM; |
| 545 | |
| 546 | chip->cl = client; |
| 547 | chip->pdata = pdata; |
Milo(Woogyom) Kim | 48068d5 | 2013-02-05 18:08:49 +0900 | [diff] [blame] | 548 | chip->cfg = &lp5523_cfg; |
Milo(Woogyom) Kim | 6a0c9a4 | 2013-02-05 18:03:08 +0900 | [diff] [blame] | 549 | |
| 550 | mutex_init(&chip->lock); |
| 551 | |
| 552 | i2c_set_clientdata(client, led); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 553 | |
Milo(Woogyom) Kim | 22ebeb4 | 2013-02-05 18:58:35 +0900 | [diff] [blame] | 554 | ret = lp55xx_init_device(chip); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 555 | if (ret) |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 556 | goto err_init; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 557 | |
Kim, Milo | 56a1e9a | 2012-09-04 15:06:26 +0800 | [diff] [blame] | 558 | dev_info(&client->dev, "%s Programmable led chip found\n", id->name); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 559 | |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 560 | ret = lp55xx_register_leds(led, chip); |
Milo(Woogyom) Kim | f652480 | 2013-02-05 17:53:40 +0900 | [diff] [blame] | 561 | if (ret) |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 562 | goto err_register_leds; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 563 | |
| 564 | ret = lp5523_register_sysfs(client); |
| 565 | if (ret) { |
| 566 | dev_err(&client->dev, "registering sysfs failed\n"); |
Bryan Wu | 94ca4bc | 2012-07-04 11:44:18 +0800 | [diff] [blame] | 567 | goto fail2; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 568 | } |
| 569 | return ret; |
Bryan Wu | 94ca4bc | 2012-07-04 11:44:18 +0800 | [diff] [blame] | 570 | fail2: |
Milo(Woogyom) Kim | c3a68eb | 2013-02-05 19:11:18 +0900 | [diff] [blame] | 571 | lp55xx_unregister_leds(led, chip); |
Milo(Woogyom) Kim | 9e9b3db | 2013-02-05 19:06:27 +0900 | [diff] [blame] | 572 | err_register_leds: |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 573 | lp55xx_deinit_device(chip); |
Milo(Woogyom) Kim | f6c64c6 | 2013-02-05 17:58:01 +0900 | [diff] [blame] | 574 | err_init: |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 575 | return ret; |
| 576 | } |
| 577 | |
| 578 | static int lp5523_remove(struct i2c_client *client) |
| 579 | { |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 580 | struct lp55xx_led *led = i2c_get_clientdata(client); |
| 581 | struct lp55xx_chip *chip = led->chip; |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 582 | |
Kim, Milo | 23301b7 | 2012-09-12 20:16:00 +0800 | [diff] [blame] | 583 | /* Disable engine mode */ |
| 584 | lp5523_write(client, LP5523_REG_OP_MODE, LP5523_CMD_DISABLED); |
| 585 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 586 | lp5523_unregister_sysfs(client); |
| 587 | |
Milo(Woogyom) Kim | c3a68eb | 2013-02-05 19:11:18 +0900 | [diff] [blame] | 588 | lp55xx_unregister_leds(led, chip); |
Milo(Woogyom) Kim | 6ce6176 | 2013-02-05 19:03:02 +0900 | [diff] [blame] | 589 | lp55xx_deinit_device(chip); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 590 | |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static const struct i2c_device_id lp5523_id[] = { |
Kim, Milo | 27d7704 | 2012-09-04 15:06:18 +0800 | [diff] [blame] | 595 | { "lp5523", LP5523 }, |
| 596 | { "lp55231", LP55231 }, |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 597 | { } |
| 598 | }; |
| 599 | |
| 600 | MODULE_DEVICE_TABLE(i2c, lp5523_id); |
| 601 | |
| 602 | static struct i2c_driver lp5523_driver = { |
| 603 | .driver = { |
Kim, Milo | 27d7704 | 2012-09-04 15:06:18 +0800 | [diff] [blame] | 604 | .name = "lp5523x", |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 605 | }, |
| 606 | .probe = lp5523_probe, |
| 607 | .remove = lp5523_remove, |
| 608 | .id_table = lp5523_id, |
| 609 | }; |
| 610 | |
Axel Lin | 09a0d18 | 2012-01-10 15:09:27 -0800 | [diff] [blame] | 611 | module_i2c_driver(lp5523_driver); |
Samu Onkalo | 0efba16 | 2010-11-11 14:05:22 -0800 | [diff] [blame] | 612 | |
| 613 | MODULE_AUTHOR("Mathias Nyman <mathias.nyman@nokia.com>"); |
| 614 | MODULE_DESCRIPTION("LP5523 LED engine"); |
| 615 | MODULE_LICENSE("GPL"); |