-
Notifications
You must be signed in to change notification settings - Fork 1
/
cat3626.c
executable file
·271 lines (217 loc) · 7.59 KB
/
cat3626.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
cat3626.c - high efficiency 1x/1.5x fractional charge pump with
programmable dimming current in six LED channels
Doug Szumski <d.s.szumski@gmail.com> 2010-03-29
based on ds1621.c by Christian W. Zuckschwerdt <zany@triq.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/err.h>
#include <linux/mutex.h>
#include <linux/sysfs.h>
/* The CAT3626 can only exist at 0x66 */
static const unsigned short normal_i2c[] = { 0x66, I2C_CLIENT_END };
/* Insmod parameters */
I2C_CLIENT_INSMOD_1(CAT3626);
/* CAT3626 level registers */
static const u8 CAT3626_REG_LEVEL[3] = {
0x00, /* CHANNEL A: RED */
0x01, /* CHANNEL B: GREEN */
0x02, /* CHANNEL C: BLUE */
};
/* CAT3626 channel enable data */
static const u8 CAT3626_CHN_ENABLE[3] = {
0x02, /* CHANNEL A2: RED */
0x08, /* CHANNEL B2: GREEN */
0x10, /* CHANNEL C1: BLUE */
};
/*CAT3626 output control register*/
#define CAT3626_ENA 0x03
/*CAT3626 chip register settings */
/*NOTE: this enables channels A2,B2 and C1*/
#define CAT3626_ENA_CFG 0x1A
#define CAT3626_DIS_CFG 0x00
/*Constrain the maximum channel output to 20mA (limit of the LED)*/
#define CAT3626_MAX_BRI 0x27
#define CAT3626_MIN_BRI 0x00
/*Level and channel data*/
struct CAT3626_data {
struct device *hwmon_dev;
struct mutex update_lock;
u8 level[3]; /* level values, byte */
u8 channel[3]; /* channel values, byte, should be boolean */
};
static int CAT3626_write_level(struct i2c_client *client, u8 reg, u8 value)
{
return i2c_smbus_write_byte_data(client, reg, value);
}
static void CAT3626_init_client(struct i2c_client *client)
{
/*Turn all channels off on startup */
i2c_smbus_write_byte_data(client, CAT3626_ENA, CAT3626_DIS_CFG);
}
static ssize_t show_level(struct device *dev, struct device_attribute *da,
char *buf)
{
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
struct i2c_client *client = to_i2c_client(dev);
struct CAT3626_data *data = i2c_get_clientdata(client);
return sprintf(buf, "%d\n", data->level[attr->index]);
}
static ssize_t set_level(struct device *dev, struct device_attribute *da,
const char *buf, size_t count)
{
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
struct i2c_client *client = to_i2c_client(dev);
struct CAT3626_data *data = i2c_get_clientdata(client);
/* Read the user level input and constrain to the LED capabilities */
u8 level_input = simple_strtol(buf, NULL, 10);
u8 val = SENSORS_LIMIT(level_input, CAT3626_MIN_BRI, CAT3626_MAX_BRI);
/* Update the LED level */
mutex_lock(&data->update_lock);
data->level[attr->index] = val;
CAT3626_write_level(client, CAT3626_REG_LEVEL[attr->index],
data->level[attr->index]);
mutex_unlock(&data->update_lock);
return count;
}
static ssize_t set_channel(struct device *dev, struct device_attribute *da,
const char *buf, size_t count)
{
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
struct i2c_client *client = to_i2c_client(dev);
struct CAT3626_data *data = i2c_get_clientdata(client);
u8 conf, new_conf;
/* Read the user channel input and constrain to boolean (must be a better way?) */
u8 channel_input = simple_strtol(buf, NULL, 10);
u8 val = SENSORS_LIMIT(channel_input, 0, 1);
/* Update the LED channel */
mutex_lock(&data->update_lock);
data->channel[attr->index] = val;
new_conf = conf = i2c_smbus_read_byte_data(client, CAT3626_ENA);
if (val == 0)
new_conf &= ~CAT3626_CHN_ENABLE[attr->index];
else if (val == 1)
new_conf |= CAT3626_CHN_ENABLE[attr->index];
if (conf != new_conf)
CAT3626_write_level(client, CAT3626_ENA, new_conf);
mutex_unlock(&data->update_lock);
return count;
}
static ssize_t show_channel(struct device *dev, struct device_attribute *da,
char *buf)
{
struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
struct i2c_client *client = to_i2c_client(dev);
struct CAT3626_data *data = i2c_get_clientdata(client);
return sprintf(buf, "%d\n", data->channel[attr->index]);
}
static SENSOR_DEVICE_ATTR(red_level, S_IWUSR | S_IRUGO, show_level, set_level, 0);
static SENSOR_DEVICE_ATTR(grn_level, S_IWUSR | S_IRUGO, show_level, set_level, 1);
static SENSOR_DEVICE_ATTR(blu_level, S_IWUSR | S_IRUGO, show_level, set_level, 2);
static SENSOR_DEVICE_ATTR(red_channel, S_IWUSR | S_IRUGO, show_channel, set_channel, 0);
static SENSOR_DEVICE_ATTR(grn_channel, S_IWUSR | S_IRUGO, show_channel, set_channel, 1);
static SENSOR_DEVICE_ATTR(blu_channel, S_IWUSR | S_IRUGO, show_channel, set_channel, 2);
static struct attribute *CAT3626_attributes[] = {
&sensor_dev_attr_red_level.dev_attr.attr,
&sensor_dev_attr_grn_level.dev_attr.attr,
&sensor_dev_attr_blu_level.dev_attr.attr,
&sensor_dev_attr_red_channel.dev_attr.attr,
&sensor_dev_attr_grn_channel.dev_attr.attr,
&sensor_dev_attr_blu_channel.dev_attr.attr,
NULL
};
static const struct attribute_group CAT3626_group = {
.attrs = CAT3626_attributes,
};
static int CAT3626_detect(struct i2c_client *client, int kind,
struct i2c_board_info *info)
{
/*add a detection routine here - currently the chip is always found! */
strlcpy(info->type, "CAT3626", I2C_NAME_SIZE);
return 0;
}
static int CAT3626_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct CAT3626_data *data;
int err;
data = kzalloc(sizeof(struct CAT3626_data), GFP_KERNEL);
if (!data) {
err = -ENOMEM;
goto exit;
}
i2c_set_clientdata(client, data);
mutex_init(&data->update_lock);
/* Initialize the CAT3626 chip */
CAT3626_init_client(client);
/* Register sysfs hooks */
if ((err = sysfs_create_group(&client->dev.kobj, &CAT3626_group)))
goto exit_free;
data->hwmon_dev = hwmon_device_register(&client->dev);
if (IS_ERR(data->hwmon_dev)) {
err = PTR_ERR(data->hwmon_dev);
goto exit_remove_files;
}
return 0;
exit_remove_files:
sysfs_remove_group(&client->dev.kobj, &CAT3626_group);
exit_free:
kfree(data);
exit:
return err;
}
static int CAT3626_remove(struct i2c_client *client)
{
struct CAT3626_data *data = i2c_get_clientdata(client);
hwmon_device_unregister(data->hwmon_dev);
sysfs_remove_group(&client->dev.kobj, &CAT3626_group);
kfree(data);
return 0;
}
static const struct i2c_device_id CAT3626_id[] = {
{ "CAT3626", CAT3626 },
{ }
};
MODULE_DEVICE_TABLE(i2c, CAT3626_id);
/* This is the driver that will be inserted */
static struct i2c_driver CAT3626_driver = {
.class = I2C_CLASS_HWMON,
.driver = {
.name = "CAT3626",
},
.probe = CAT3626_probe,
.remove = CAT3626_remove,
.id_table = CAT3626_id,
.detect = CAT3626_detect,
.address_data = &addr_data,
};
static int __init CAT3626_init(void)
{
return i2c_add_driver(&CAT3626_driver);
}
static void __exit CAT3626_exit(void)
{
i2c_del_driver(&CAT3626_driver);
}
MODULE_AUTHOR("Doug Szumski <d.s.szumski@gmail.com>");
MODULE_DESCRIPTION("CAT3626 RGB LED driver");
MODULE_LICENSE("GPL");
module_init(CAT3626_init);
module_exit(CAT3626_exit);