Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 1 | /* |
| 2 | * pca9532.c - 16-bit Led dimmer |
| 3 | * |
| 4 | * Copyright (C) 2008 Riku Voipio <riku.voipio@movial.fi> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * Datasheet: http://www.nxp.com/acrobat/datasheets/PCA9532_3.pdf |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/i2c.h> |
| 16 | #include <linux/leds.h> |
| 17 | #include <linux/input.h> |
| 18 | #include <linux/mutex.h> |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 19 | #include <linux/workqueue.h> |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 20 | #include <linux/leds-pca9532.h> |
| 21 | |
| 22 | static const unsigned short normal_i2c[] = { /*0x60,*/ I2C_CLIENT_END}; |
| 23 | I2C_CLIENT_INSMOD_1(pca9532); |
| 24 | |
| 25 | #define PCA9532_REG_PSC(i) (0x2+(i)*2) |
| 26 | #define PCA9532_REG_PWM(i) (0x3+(i)*2) |
| 27 | #define PCA9532_REG_LS0 0x6 |
| 28 | #define LED_REG(led) ((led>>2)+PCA9532_REG_LS0) |
| 29 | #define LED_NUM(led) (led & 0x3) |
| 30 | |
| 31 | #define ldev_to_led(c) container_of(c, struct pca9532_led, ldev) |
| 32 | |
| 33 | struct pca9532_data { |
| 34 | struct i2c_client *client; |
| 35 | struct pca9532_led leds[16]; |
| 36 | struct mutex update_lock; |
| 37 | struct input_dev *idev; |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 38 | struct work_struct work; |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 39 | u8 pwm[2]; |
| 40 | u8 psc[2]; |
| 41 | }; |
| 42 | |
| 43 | static int pca9532_probe(struct i2c_client *client, |
| 44 | const struct i2c_device_id *id); |
| 45 | static int pca9532_remove(struct i2c_client *client); |
| 46 | |
| 47 | static const struct i2c_device_id pca9532_id[] = { |
| 48 | { "pca9532", 0 }, |
| 49 | { } |
| 50 | }; |
| 51 | |
| 52 | MODULE_DEVICE_TABLE(i2c, pca9532_id); |
| 53 | |
| 54 | static struct i2c_driver pca9532_driver = { |
| 55 | .driver = { |
| 56 | .name = "pca9532", |
| 57 | }, |
| 58 | .probe = pca9532_probe, |
| 59 | .remove = pca9532_remove, |
| 60 | .id_table = pca9532_id, |
| 61 | }; |
| 62 | |
| 63 | /* We have two pwm/blinkers, but 16 possible leds to drive. Additionaly, |
| 64 | * the clever Thecus people are using one pwm to drive the beeper. So, |
| 65 | * as a compromise we average one pwm to the values requested by all |
| 66 | * leds that are not ON/OFF. |
| 67 | * */ |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 68 | static int pca9532_calcpwm(struct i2c_client *client, int pwm, int blink, |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 69 | enum led_brightness value) |
| 70 | { |
| 71 | int a = 0, b = 0, i = 0; |
| 72 | struct pca9532_data *data = i2c_get_clientdata(client); |
| 73 | for (i = 0; i < 16; i++) { |
| 74 | if (data->leds[i].type == PCA9532_TYPE_LED && |
| 75 | data->leds[i].state == PCA9532_PWM0+pwm) { |
| 76 | a++; |
| 77 | b += data->leds[i].ldev.brightness; |
| 78 | } |
| 79 | } |
| 80 | if (a == 0) { |
| 81 | dev_err(&client->dev, |
| 82 | "fear of division by zero %d/%d, wanted %d\n", |
| 83 | b, a, value); |
| 84 | return -EINVAL; |
| 85 | } |
| 86 | b = b/a; |
| 87 | if (b > 0xFF) |
| 88 | return -EINVAL; |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 89 | data->pwm[pwm] = b; |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 90 | data->psc[pwm] = blink; |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int pca9532_setpwm(struct i2c_client *client, int pwm) |
| 95 | { |
| 96 | struct pca9532_data *data = i2c_get_clientdata(client); |
| 97 | mutex_lock(&data->update_lock); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 98 | i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(pwm), |
| 99 | data->pwm[pwm]); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 100 | i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(pwm), |
| 101 | data->psc[pwm]); |
| 102 | mutex_unlock(&data->update_lock); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | /* Set LED routing */ |
| 107 | static void pca9532_setled(struct pca9532_led *led) |
| 108 | { |
| 109 | struct i2c_client *client = led->client; |
| 110 | struct pca9532_data *data = i2c_get_clientdata(client); |
| 111 | char reg; |
| 112 | |
| 113 | mutex_lock(&data->update_lock); |
| 114 | reg = i2c_smbus_read_byte_data(client, LED_REG(led->id)); |
| 115 | /* zero led bits */ |
| 116 | reg = reg & ~(0x3<<LED_NUM(led->id)*2); |
| 117 | /* set the new value */ |
| 118 | reg = reg | (led->state << LED_NUM(led->id)*2); |
| 119 | i2c_smbus_write_byte_data(client, LED_REG(led->id), reg); |
| 120 | mutex_unlock(&data->update_lock); |
| 121 | } |
| 122 | |
| 123 | static void pca9532_set_brightness(struct led_classdev *led_cdev, |
| 124 | enum led_brightness value) |
| 125 | { |
| 126 | int err = 0; |
| 127 | struct pca9532_led *led = ldev_to_led(led_cdev); |
| 128 | |
| 129 | if (value == LED_OFF) |
| 130 | led->state = PCA9532_OFF; |
| 131 | else if (value == LED_FULL) |
| 132 | led->state = PCA9532_ON; |
| 133 | else { |
| 134 | led->state = PCA9532_PWM0; /* Thecus: hardcode one pwm */ |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 135 | err = pca9532_calcpwm(led->client, 0, 0, value); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 136 | if (err) |
| 137 | return; /* XXX: led api doesn't allow error code? */ |
| 138 | } |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 139 | schedule_work(&led->work); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static int pca9532_set_blink(struct led_classdev *led_cdev, |
| 143 | unsigned long *delay_on, unsigned long *delay_off) |
| 144 | { |
| 145 | struct pca9532_led *led = ldev_to_led(led_cdev); |
| 146 | struct i2c_client *client = led->client; |
| 147 | int psc; |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 148 | int err = 0; |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 149 | |
| 150 | if (*delay_on == 0 && *delay_off == 0) { |
| 151 | /* led subsystem ask us for a blink rate */ |
| 152 | *delay_on = 1000; |
| 153 | *delay_off = 1000; |
| 154 | } |
| 155 | if (*delay_on != *delay_off || *delay_on > 1690 || *delay_on < 6) |
| 156 | return -EINVAL; |
| 157 | |
| 158 | /* Thecus specific: only use PSC/PWM 0 */ |
| 159 | psc = (*delay_on * 152-1)/1000; |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 160 | err = pca9532_calcpwm(client, 0, psc, led_cdev->brightness); |
| 161 | if (err) |
| 162 | return err; |
| 163 | schedule_work(&led->work); |
| 164 | return 0; |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Sven Wegener | 0d73357 | 2008-11-17 14:33:41 +0000 | [diff] [blame] | 167 | static int pca9532_event(struct input_dev *dev, unsigned int type, |
| 168 | unsigned int code, int value) |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 169 | { |
| 170 | struct pca9532_data *data = input_get_drvdata(dev); |
| 171 | |
| 172 | if (type != EV_SND && (code != SND_BELL || code != SND_TONE)) |
| 173 | return -1; |
| 174 | |
| 175 | /* XXX: allow different kind of beeps with psc/pwm modifications */ |
| 176 | if (value > 1 && value < 32767) |
| 177 | data->pwm[1] = 127; |
| 178 | else |
| 179 | data->pwm[1] = 0; |
| 180 | |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 181 | schedule_work(&data->work); |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static void pca9532_input_work(struct work_struct *work) |
| 187 | { |
| 188 | struct pca9532_data *data; |
| 189 | data = container_of(work, struct pca9532_data, work); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 190 | mutex_lock(&data->update_lock); |
| 191 | i2c_smbus_write_byte_data(data->client, PCA9532_REG_PWM(1), |
| 192 | data->pwm[1]); |
| 193 | mutex_unlock(&data->update_lock); |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 194 | } |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 195 | |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 196 | static void pca9532_led_work(struct work_struct *work) |
| 197 | { |
| 198 | struct pca9532_led *led; |
| 199 | led = container_of(work, struct pca9532_led, work); |
| 200 | if (led->state == PCA9532_PWM0) |
| 201 | pca9532_setpwm(led->client, 0); |
| 202 | pca9532_setled(led); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static int pca9532_configure(struct i2c_client *client, |
| 206 | struct pca9532_data *data, struct pca9532_platform_data *pdata) |
| 207 | { |
| 208 | int i, err = 0; |
| 209 | |
| 210 | for (i = 0; i < 2; i++) { |
| 211 | data->pwm[i] = pdata->pwm[i]; |
| 212 | data->psc[i] = pdata->psc[i]; |
| 213 | i2c_smbus_write_byte_data(client, PCA9532_REG_PWM(i), |
| 214 | data->pwm[i]); |
| 215 | i2c_smbus_write_byte_data(client, PCA9532_REG_PSC(i), |
| 216 | data->psc[i]); |
| 217 | } |
| 218 | |
| 219 | for (i = 0; i < 16; i++) { |
| 220 | struct pca9532_led *led = &data->leds[i]; |
| 221 | struct pca9532_led *pled = &pdata->leds[i]; |
| 222 | led->client = client; |
| 223 | led->id = i; |
| 224 | led->type = pled->type; |
| 225 | switch (led->type) { |
| 226 | case PCA9532_TYPE_NONE: |
| 227 | break; |
| 228 | case PCA9532_TYPE_LED: |
| 229 | led->state = pled->state; |
| 230 | led->name = pled->name; |
| 231 | led->ldev.name = led->name; |
| 232 | led->ldev.brightness = LED_OFF; |
| 233 | led->ldev.brightness_set = pca9532_set_brightness; |
| 234 | led->ldev.blink_set = pca9532_set_blink; |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 235 | INIT_WORK(&led->work, pca9532_led_work); |
Sven Wegener | f785d02 | 2008-12-03 08:12:53 +0000 | [diff] [blame] | 236 | err = led_classdev_register(&client->dev, &led->ldev); |
| 237 | if (err < 0) { |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 238 | dev_err(&client->dev, |
| 239 | "couldn't register LED %s\n", |
| 240 | led->name); |
| 241 | goto exit; |
| 242 | } |
| 243 | pca9532_setled(led); |
| 244 | break; |
| 245 | case PCA9532_TYPE_N2100_BEEP: |
| 246 | BUG_ON(data->idev); |
| 247 | led->state = PCA9532_PWM1; |
| 248 | pca9532_setled(led); |
| 249 | data->idev = input_allocate_device(); |
| 250 | if (data->idev == NULL) { |
| 251 | err = -ENOMEM; |
| 252 | goto exit; |
| 253 | } |
| 254 | data->idev->name = pled->name; |
| 255 | data->idev->phys = "i2c/pca9532"; |
| 256 | data->idev->id.bustype = BUS_HOST; |
| 257 | data->idev->id.vendor = 0x001f; |
| 258 | data->idev->id.product = 0x0001; |
| 259 | data->idev->id.version = 0x0100; |
| 260 | data->idev->evbit[0] = BIT_MASK(EV_SND); |
| 261 | data->idev->sndbit[0] = BIT_MASK(SND_BELL) | |
| 262 | BIT_MASK(SND_TONE); |
| 263 | data->idev->event = pca9532_event; |
| 264 | input_set_drvdata(data->idev, data); |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 265 | INIT_WORK(&data->work, pca9532_input_work); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 266 | err = input_register_device(data->idev); |
| 267 | if (err) { |
| 268 | input_free_device(data->idev); |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 269 | cancel_work_sync(&data->work); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 270 | data->idev = NULL; |
| 271 | goto exit; |
| 272 | } |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | return 0; |
| 277 | |
| 278 | exit: |
| 279 | if (i > 0) |
| 280 | for (i = i - 1; i >= 0; i--) |
| 281 | switch (data->leds[i].type) { |
| 282 | case PCA9532_TYPE_NONE: |
| 283 | break; |
| 284 | case PCA9532_TYPE_LED: |
| 285 | led_classdev_unregister(&data->leds[i].ldev); |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 286 | cancel_work_sync(&data->leds[i].work); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 287 | break; |
| 288 | case PCA9532_TYPE_N2100_BEEP: |
| 289 | if (data->idev != NULL) { |
| 290 | input_unregister_device(data->idev); |
| 291 | input_free_device(data->idev); |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 292 | cancel_work_sync(&data->work); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 293 | data->idev = NULL; |
| 294 | } |
| 295 | break; |
| 296 | } |
| 297 | |
| 298 | return err; |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static int pca9532_probe(struct i2c_client *client, |
| 302 | const struct i2c_device_id *id) |
| 303 | { |
| 304 | struct pca9532_data *data = i2c_get_clientdata(client); |
| 305 | struct pca9532_platform_data *pca9532_pdata = client->dev.platform_data; |
Sven Wegener | f785d02 | 2008-12-03 08:12:53 +0000 | [diff] [blame] | 306 | int err; |
| 307 | |
| 308 | if (!pca9532_pdata) |
| 309 | return -EIO; |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 310 | |
| 311 | if (!i2c_check_functionality(client->adapter, |
| 312 | I2C_FUNC_SMBUS_BYTE_DATA)) |
| 313 | return -EIO; |
| 314 | |
Sven Wegener | f785d02 | 2008-12-03 08:12:53 +0000 | [diff] [blame] | 315 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 316 | if (!data) |
| 317 | return -ENOMEM; |
| 318 | |
| 319 | dev_info(&client->dev, "setting platform data\n"); |
| 320 | i2c_set_clientdata(client, data); |
| 321 | data->client = client; |
| 322 | mutex_init(&data->update_lock); |
| 323 | |
Sven Wegener | f785d02 | 2008-12-03 08:12:53 +0000 | [diff] [blame] | 324 | err = pca9532_configure(client, data, pca9532_pdata); |
| 325 | if (err) { |
| 326 | kfree(data); |
| 327 | i2c_set_clientdata(client, NULL); |
| 328 | } |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 329 | |
Sven Wegener | f785d02 | 2008-12-03 08:12:53 +0000 | [diff] [blame] | 330 | return err; |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static int pca9532_remove(struct i2c_client *client) |
| 334 | { |
| 335 | struct pca9532_data *data = i2c_get_clientdata(client); |
| 336 | int i; |
| 337 | for (i = 0; i < 16; i++) |
| 338 | switch (data->leds[i].type) { |
| 339 | case PCA9532_TYPE_NONE: |
| 340 | break; |
| 341 | case PCA9532_TYPE_LED: |
| 342 | led_classdev_unregister(&data->leds[i].ldev); |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 343 | cancel_work_sync(&data->leds[i].work); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 344 | break; |
| 345 | case PCA9532_TYPE_N2100_BEEP: |
| 346 | if (data->idev != NULL) { |
| 347 | input_unregister_device(data->idev); |
| 348 | input_free_device(data->idev); |
Riku Voipio | 934cd3f | 2008-12-03 08:21:36 +0000 | [diff] [blame] | 349 | cancel_work_sync(&data->work); |
Riku Voipio | e14fa82 | 2008-05-31 14:43:41 +0100 | [diff] [blame] | 350 | data->idev = NULL; |
| 351 | } |
| 352 | break; |
| 353 | } |
| 354 | |
| 355 | kfree(data); |
| 356 | i2c_set_clientdata(client, NULL); |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | static int __init pca9532_init(void) |
| 361 | { |
| 362 | return i2c_add_driver(&pca9532_driver); |
| 363 | } |
| 364 | |
| 365 | static void __exit pca9532_exit(void) |
| 366 | { |
| 367 | i2c_del_driver(&pca9532_driver); |
| 368 | } |
| 369 | |
| 370 | MODULE_AUTHOR("Riku Voipio <riku.voipio@movial.fi>"); |
| 371 | MODULE_LICENSE("GPL"); |
| 372 | MODULE_DESCRIPTION("PCA 9532 LED dimmer"); |
| 373 | |
| 374 | module_init(pca9532_init); |
| 375 | module_exit(pca9532_exit); |
| 376 | |