Peter Meerwald | 75cb2e1 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 bct electronic GmbH |
| 3 | * |
| 4 | * Author: Peter Meerwald <p.meerwald@bct-electronic.com> |
| 5 | * |
| 6 | * Based on leds-pca955x.c |
| 7 | * |
| 8 | * This file is subject to the terms and conditions of version 2 of |
| 9 | * the GNU General Public License. See the file COPYING in the main |
| 10 | * directory of this archive for more details. |
| 11 | * |
| 12 | * LED driver for the PCA9633 I2C LED driver (7-bit slave address 0x62) |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/string.h> |
| 19 | #include <linux/ctype.h> |
| 20 | #include <linux/leds.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/i2c.h> |
| 23 | #include <linux/workqueue.h> |
| 24 | #include <linux/slab.h> |
Peter Meerwald | 2f73c392 | 2012-09-24 14:53:49 +0800 | [diff] [blame] | 25 | #include <linux/platform_data/leds-pca9633.h> |
Peter Meerwald | 75cb2e1 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 26 | |
| 27 | /* LED select registers determine the source that drives LED outputs */ |
| 28 | #define PCA9633_LED_OFF 0x0 /* LED driver off */ |
| 29 | #define PCA9633_LED_ON 0x1 /* LED driver on */ |
| 30 | #define PCA9633_LED_PWM 0x2 /* Controlled through PWM */ |
| 31 | #define PCA9633_LED_GRP_PWM 0x3 /* Controlled through PWM/GRPPWM */ |
| 32 | |
| 33 | #define PCA9633_MODE1 0x00 |
| 34 | #define PCA9633_MODE2 0x01 |
| 35 | #define PCA9633_PWM_BASE 0x02 |
| 36 | #define PCA9633_LEDOUT 0x08 |
| 37 | |
| 38 | static const struct i2c_device_id pca9633_id[] = { |
| 39 | { "pca9633", 0 }, |
| 40 | { } |
| 41 | }; |
| 42 | MODULE_DEVICE_TABLE(i2c, pca9633_id); |
| 43 | |
| 44 | struct pca9633_led { |
| 45 | struct i2c_client *client; |
| 46 | struct work_struct work; |
| 47 | enum led_brightness brightness; |
| 48 | struct led_classdev led_cdev; |
| 49 | int led_num; /* 0 .. 3 potentially */ |
| 50 | char name[32]; |
| 51 | }; |
| 52 | |
| 53 | static void pca9633_led_work(struct work_struct *work) |
| 54 | { |
| 55 | struct pca9633_led *pca9633 = container_of(work, |
| 56 | struct pca9633_led, work); |
| 57 | u8 ledout = i2c_smbus_read_byte_data(pca9633->client, PCA9633_LEDOUT); |
| 58 | int shift = 2 * pca9633->led_num; |
| 59 | u8 mask = 0x3 << shift; |
| 60 | |
| 61 | switch (pca9633->brightness) { |
| 62 | case LED_FULL: |
| 63 | i2c_smbus_write_byte_data(pca9633->client, PCA9633_LEDOUT, |
| 64 | (ledout & ~mask) | (PCA9633_LED_ON << shift)); |
| 65 | break; |
| 66 | case LED_OFF: |
| 67 | i2c_smbus_write_byte_data(pca9633->client, PCA9633_LEDOUT, |
| 68 | ledout & ~mask); |
| 69 | break; |
| 70 | default: |
| 71 | i2c_smbus_write_byte_data(pca9633->client, |
| 72 | PCA9633_PWM_BASE + pca9633->led_num, |
| 73 | pca9633->brightness); |
| 74 | i2c_smbus_write_byte_data(pca9633->client, PCA9633_LEDOUT, |
| 75 | (ledout & ~mask) | (PCA9633_LED_PWM << shift)); |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static void pca9633_led_set(struct led_classdev *led_cdev, |
| 81 | enum led_brightness value) |
| 82 | { |
| 83 | struct pca9633_led *pca9633; |
| 84 | |
| 85 | pca9633 = container_of(led_cdev, struct pca9633_led, led_cdev); |
| 86 | |
| 87 | pca9633->brightness = value; |
| 88 | |
| 89 | /* |
| 90 | * Must use workqueue for the actual I/O since I2C operations |
| 91 | * can sleep. |
| 92 | */ |
| 93 | schedule_work(&pca9633->work); |
| 94 | } |
| 95 | |
Bill Pemberton | 98ea1ea | 2012-11-19 13:23:02 -0500 | [diff] [blame] | 96 | static int pca9633_probe(struct i2c_client *client, |
Peter Meerwald | 75cb2e1 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 97 | const struct i2c_device_id *id) |
| 98 | { |
| 99 | struct pca9633_led *pca9633; |
Peter Meerwald | 2f73c392 | 2012-09-24 14:53:49 +0800 | [diff] [blame] | 100 | struct pca9633_platform_data *pdata; |
Peter Meerwald | 75cb2e1 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 101 | int i, err; |
| 102 | |
Peter Meerwald | 75cb2e1 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 103 | pdata = client->dev.platform_data; |
| 104 | |
| 105 | if (pdata) { |
Peter Meerwald | 2f73c392 | 2012-09-24 14:53:49 +0800 | [diff] [blame] | 106 | if (pdata->leds.num_leds <= 0 || pdata->leds.num_leds > 4) { |
Peter Meerwald | 75cb2e1 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 107 | dev_err(&client->dev, "board info must claim at most 4 LEDs"); |
| 108 | return -EINVAL; |
| 109 | } |
| 110 | } |
| 111 | |
Bryan Wu | 983ce88 | 2012-07-04 11:40:10 +0800 | [diff] [blame] | 112 | pca9633 = devm_kzalloc(&client->dev, 4 * sizeof(*pca9633), GFP_KERNEL); |
Peter Meerwald | 75cb2e1 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 113 | if (!pca9633) |
| 114 | return -ENOMEM; |
| 115 | |
| 116 | i2c_set_clientdata(client, pca9633); |
| 117 | |
| 118 | for (i = 0; i < 4; i++) { |
| 119 | pca9633[i].client = client; |
| 120 | pca9633[i].led_num = i; |
| 121 | |
| 122 | /* Platform data can specify LED names and default triggers */ |
Peter Meerwald | 2f73c392 | 2012-09-24 14:53:49 +0800 | [diff] [blame] | 123 | if (pdata && i < pdata->leds.num_leds) { |
| 124 | if (pdata->leds.leds[i].name) |
Peter Meerwald | 75cb2e1 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 125 | snprintf(pca9633[i].name, |
| 126 | sizeof(pca9633[i].name), "pca9633:%s", |
Peter Meerwald | 2f73c392 | 2012-09-24 14:53:49 +0800 | [diff] [blame] | 127 | pdata->leds.leds[i].name); |
| 128 | if (pdata->leds.leds[i].default_trigger) |
Peter Meerwald | 75cb2e1 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 129 | pca9633[i].led_cdev.default_trigger = |
Peter Meerwald | 2f73c392 | 2012-09-24 14:53:49 +0800 | [diff] [blame] | 130 | pdata->leds.leds[i].default_trigger; |
Peter Meerwald | 75cb2e1 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 131 | } else { |
| 132 | snprintf(pca9633[i].name, sizeof(pca9633[i].name), |
| 133 | "pca9633:%d", i); |
| 134 | } |
| 135 | |
| 136 | pca9633[i].led_cdev.name = pca9633[i].name; |
| 137 | pca9633[i].led_cdev.brightness_set = pca9633_led_set; |
| 138 | |
| 139 | INIT_WORK(&pca9633[i].work, pca9633_led_work); |
| 140 | |
| 141 | err = led_classdev_register(&client->dev, &pca9633[i].led_cdev); |
| 142 | if (err < 0) |
| 143 | goto exit; |
| 144 | } |
| 145 | |
| 146 | /* Disable LED all-call address and set normal mode */ |
| 147 | i2c_smbus_write_byte_data(client, PCA9633_MODE1, 0x00); |
| 148 | |
Peter Meerwald | 2f73c392 | 2012-09-24 14:53:49 +0800 | [diff] [blame] | 149 | /* Configure output: open-drain or totem pole (push-pull) */ |
| 150 | if (pdata && pdata->outdrv == PCA9633_OPEN_DRAIN) |
| 151 | i2c_smbus_write_byte_data(client, PCA9633_MODE2, 0x01); |
| 152 | |
Peter Meerwald | 75cb2e1 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 153 | /* Turn off LEDs */ |
| 154 | i2c_smbus_write_byte_data(client, PCA9633_LEDOUT, 0x00); |
| 155 | |
| 156 | return 0; |
| 157 | |
| 158 | exit: |
| 159 | while (i--) { |
| 160 | led_classdev_unregister(&pca9633[i].led_cdev); |
| 161 | cancel_work_sync(&pca9633[i].work); |
| 162 | } |
| 163 | |
Peter Meerwald | 75cb2e1 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 164 | return err; |
| 165 | } |
| 166 | |
Bill Pemberton | 678e8a6 | 2012-11-19 13:26:00 -0500 | [diff] [blame] | 167 | static int pca9633_remove(struct i2c_client *client) |
Peter Meerwald | 75cb2e1 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 168 | { |
| 169 | struct pca9633_led *pca9633 = i2c_get_clientdata(client); |
| 170 | int i; |
| 171 | |
| 172 | for (i = 0; i < 4; i++) { |
| 173 | led_classdev_unregister(&pca9633[i].led_cdev); |
| 174 | cancel_work_sync(&pca9633[i].work); |
| 175 | } |
| 176 | |
Peter Meerwald | 75cb2e1 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static struct i2c_driver pca9633_driver = { |
| 181 | .driver = { |
| 182 | .name = "leds-pca9633", |
| 183 | .owner = THIS_MODULE, |
| 184 | }, |
| 185 | .probe = pca9633_probe, |
Bill Pemberton | df07cf8 | 2012-11-19 13:20:20 -0500 | [diff] [blame] | 186 | .remove = pca9633_remove, |
Peter Meerwald | 75cb2e1 | 2012-03-23 15:02:10 -0700 | [diff] [blame] | 187 | .id_table = pca9633_id, |
| 188 | }; |
| 189 | |
| 190 | module_i2c_driver(pca9633_driver); |
| 191 | |
| 192 | MODULE_AUTHOR("Peter Meerwald <p.meerwald@bct-electronic.com>"); |
| 193 | MODULE_DESCRIPTION("PCA9633 LED driver"); |
| 194 | MODULE_LICENSE("GPL v2"); |