]>
Commit | Line | Data |
---|---|---|
8b054426 DB |
1 | /* |
2 | * CM3323 - Capella Color Light Sensor | |
3 | * | |
4 | * Copyright (c) 2015, Intel Corporation. | |
5 | * | |
6 | * This file is subject to the terms and conditions of version 2 of | |
7 | * the GNU General Public License. See the file COPYING in the main | |
8 | * directory of this archive for more details. | |
9 | * | |
10 | * IIO driver for CM3323 (7-bit I2C slave address 0x10) | |
11 | * | |
12 | * TODO: calibscale to correct the lens factor | |
13 | */ | |
14 | #include <linux/module.h> | |
15 | #include <linux/init.h> | |
16 | #include <linux/i2c.h> | |
17 | #include <linux/mutex.h> | |
18 | ||
19 | #include <linux/iio/iio.h> | |
20 | #include <linux/iio/sysfs.h> | |
21 | ||
22 | #define CM3323_DRV_NAME "cm3323" | |
23 | ||
24 | #define CM3323_CMD_CONF 0x00 | |
25 | #define CM3323_CMD_RED_DATA 0x08 | |
26 | #define CM3323_CMD_GREEN_DATA 0x09 | |
27 | #define CM3323_CMD_BLUE_DATA 0x0A | |
28 | #define CM3323_CMD_CLEAR_DATA 0x0B | |
29 | ||
30 | #define CM3323_CONF_SD_BIT BIT(0) /* sensor disable */ | |
31 | #define CM3323_CONF_AF_BIT BIT(1) /* auto/manual force mode */ | |
054101c1 | 32 | #define CM3323_CONF_IT_MASK GENMASK(6, 4) |
8b054426 DB |
33 | #define CM3323_CONF_IT_SHIFT 4 |
34 | ||
35 | #define CM3323_INT_TIME_AVAILABLE "0.04 0.08 0.16 0.32 0.64 1.28" | |
36 | ||
37 | static const struct { | |
38 | int val; | |
39 | int val2; | |
40 | } cm3323_int_time[] = { | |
41 | {0, 40000}, /* 40 ms */ | |
42 | {0, 80000}, /* 80 ms */ | |
43 | {0, 160000}, /* 160 ms */ | |
44 | {0, 320000}, /* 320 ms */ | |
45 | {0, 640000}, /* 640 ms */ | |
46 | {1, 280000}, /* 1280 ms */ | |
47 | }; | |
48 | ||
49 | struct cm3323_data { | |
50 | struct i2c_client *client; | |
51 | u16 reg_conf; | |
52 | struct mutex mutex; | |
53 | }; | |
54 | ||
55 | #define CM3323_COLOR_CHANNEL(_color, _addr) { \ | |
56 | .type = IIO_INTENSITY, \ | |
57 | .modified = 1, \ | |
58 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ | |
59 | .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_INT_TIME), \ | |
60 | .channel2 = IIO_MOD_LIGHT_##_color, \ | |
61 | .address = _addr, \ | |
62 | } | |
63 | ||
64 | static const struct iio_chan_spec cm3323_channels[] = { | |
65 | CM3323_COLOR_CHANNEL(RED, CM3323_CMD_RED_DATA), | |
66 | CM3323_COLOR_CHANNEL(GREEN, CM3323_CMD_GREEN_DATA), | |
67 | CM3323_COLOR_CHANNEL(BLUE, CM3323_CMD_BLUE_DATA), | |
68 | CM3323_COLOR_CHANNEL(CLEAR, CM3323_CMD_CLEAR_DATA), | |
69 | }; | |
70 | ||
71 | static IIO_CONST_ATTR_INT_TIME_AVAIL(CM3323_INT_TIME_AVAILABLE); | |
72 | ||
73 | static struct attribute *cm3323_attributes[] = { | |
74 | &iio_const_attr_integration_time_available.dev_attr.attr, | |
75 | NULL | |
76 | }; | |
77 | ||
78 | static const struct attribute_group cm3323_attribute_group = { | |
79 | .attrs = cm3323_attributes, | |
80 | }; | |
81 | ||
82 | static int cm3323_init(struct iio_dev *indio_dev) | |
83 | { | |
84 | int ret; | |
85 | struct cm3323_data *data = iio_priv(indio_dev); | |
86 | ||
87 | ret = i2c_smbus_read_word_data(data->client, CM3323_CMD_CONF); | |
88 | if (ret < 0) { | |
89 | dev_err(&data->client->dev, "Error reading reg_conf\n"); | |
90 | return ret; | |
91 | } | |
92 | ||
93 | /* enable sensor and set auto force mode */ | |
94 | ret &= ~(CM3323_CONF_SD_BIT | CM3323_CONF_AF_BIT); | |
95 | ||
96 | ret = i2c_smbus_write_word_data(data->client, CM3323_CMD_CONF, ret); | |
97 | if (ret < 0) { | |
98 | dev_err(&data->client->dev, "Error writing reg_conf\n"); | |
99 | return ret; | |
100 | } | |
101 | ||
102 | data->reg_conf = ret; | |
103 | ||
104 | return 0; | |
105 | } | |
106 | ||
107 | static void cm3323_disable(struct iio_dev *indio_dev) | |
108 | { | |
109 | int ret; | |
110 | struct cm3323_data *data = iio_priv(indio_dev); | |
111 | ||
112 | ret = i2c_smbus_write_word_data(data->client, CM3323_CMD_CONF, | |
113 | CM3323_CONF_SD_BIT); | |
114 | if (ret < 0) | |
115 | dev_err(&data->client->dev, "Error writing reg_conf\n"); | |
116 | } | |
117 | ||
118 | static int cm3323_set_it_bits(struct cm3323_data *data, int val, int val2) | |
119 | { | |
120 | int i, ret; | |
121 | u16 reg_conf; | |
122 | ||
123 | for (i = 0; i < ARRAY_SIZE(cm3323_int_time); i++) { | |
124 | if (val == cm3323_int_time[i].val && | |
125 | val2 == cm3323_int_time[i].val2) { | |
c288503b | 126 | reg_conf = data->reg_conf & ~CM3323_CONF_IT_MASK; |
8b054426 DB |
127 | reg_conf |= i << CM3323_CONF_IT_SHIFT; |
128 | ||
129 | ret = i2c_smbus_write_word_data(data->client, | |
130 | CM3323_CMD_CONF, | |
131 | reg_conf); | |
132 | if (ret < 0) | |
133 | return ret; | |
134 | ||
135 | data->reg_conf = reg_conf; | |
8bf62ec8 | 136 | |
8b054426 DB |
137 | return 0; |
138 | } | |
139 | } | |
8bf62ec8 | 140 | |
8b054426 DB |
141 | return -EINVAL; |
142 | } | |
143 | ||
144 | static int cm3323_get_it_bits(struct cm3323_data *data) | |
145 | { | |
146 | int bits; | |
147 | ||
148 | bits = (data->reg_conf & CM3323_CONF_IT_MASK) >> | |
149 | CM3323_CONF_IT_SHIFT; | |
150 | ||
151 | if (bits >= ARRAY_SIZE(cm3323_int_time)) | |
152 | return -EINVAL; | |
8bf62ec8 | 153 | |
8b054426 DB |
154 | return bits; |
155 | } | |
156 | ||
157 | static int cm3323_read_raw(struct iio_dev *indio_dev, | |
158 | struct iio_chan_spec const *chan, int *val, | |
159 | int *val2, long mask) | |
160 | { | |
0ff8c78d | 161 | int ret; |
8b054426 DB |
162 | struct cm3323_data *data = iio_priv(indio_dev); |
163 | ||
164 | switch (mask) { | |
165 | case IIO_CHAN_INFO_RAW: | |
166 | mutex_lock(&data->mutex); | |
167 | ret = i2c_smbus_read_word_data(data->client, chan->address); | |
168 | if (ret < 0) { | |
169 | mutex_unlock(&data->mutex); | |
170 | return ret; | |
171 | } | |
172 | *val = ret; | |
173 | mutex_unlock(&data->mutex); | |
174 | ||
175 | return IIO_VAL_INT; | |
176 | case IIO_CHAN_INFO_INT_TIME: | |
177 | mutex_lock(&data->mutex); | |
0ff8c78d HK |
178 | ret = cm3323_get_it_bits(data); |
179 | if (ret < 0) { | |
8b054426 | 180 | mutex_unlock(&data->mutex); |
0ff8c78d | 181 | return ret; |
8b054426 DB |
182 | } |
183 | ||
0ff8c78d HK |
184 | *val = cm3323_int_time[ret].val; |
185 | *val2 = cm3323_int_time[ret].val2; | |
8b054426 DB |
186 | mutex_unlock(&data->mutex); |
187 | ||
188 | return IIO_VAL_INT_PLUS_MICRO; | |
189 | default: | |
190 | return -EINVAL; | |
191 | } | |
192 | } | |
193 | ||
194 | static int cm3323_write_raw(struct iio_dev *indio_dev, | |
195 | struct iio_chan_spec const *chan, int val, | |
196 | int val2, long mask) | |
197 | { | |
198 | struct cm3323_data *data = iio_priv(indio_dev); | |
199 | int ret; | |
200 | ||
201 | switch (mask) { | |
202 | case IIO_CHAN_INFO_INT_TIME: | |
203 | mutex_lock(&data->mutex); | |
204 | ret = cm3323_set_it_bits(data, val, val2); | |
205 | mutex_unlock(&data->mutex); | |
206 | ||
207 | return ret; | |
208 | default: | |
209 | return -EINVAL; | |
210 | } | |
211 | } | |
212 | ||
213 | static const struct iio_info cm3323_info = { | |
8b054426 DB |
214 | .read_raw = cm3323_read_raw, |
215 | .write_raw = cm3323_write_raw, | |
216 | .attrs = &cm3323_attribute_group, | |
217 | }; | |
218 | ||
219 | static int cm3323_probe(struct i2c_client *client, | |
220 | const struct i2c_device_id *id) | |
221 | { | |
222 | struct cm3323_data *data; | |
223 | struct iio_dev *indio_dev; | |
224 | int ret; | |
225 | ||
226 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); | |
227 | if (!indio_dev) | |
228 | return -ENOMEM; | |
229 | ||
230 | data = iio_priv(indio_dev); | |
231 | i2c_set_clientdata(client, indio_dev); | |
232 | data->client = client; | |
233 | ||
234 | mutex_init(&data->mutex); | |
235 | ||
236 | indio_dev->dev.parent = &client->dev; | |
237 | indio_dev->info = &cm3323_info; | |
238 | indio_dev->name = CM3323_DRV_NAME; | |
239 | indio_dev->channels = cm3323_channels; | |
240 | indio_dev->num_channels = ARRAY_SIZE(cm3323_channels); | |
241 | indio_dev->modes = INDIO_DIRECT_MODE; | |
242 | ||
243 | ret = cm3323_init(indio_dev); | |
244 | if (ret < 0) { | |
245 | dev_err(&client->dev, "cm3323 chip init failed\n"); | |
246 | return ret; | |
247 | } | |
8bf62ec8 | 248 | |
8b054426 DB |
249 | ret = iio_device_register(indio_dev); |
250 | if (ret < 0) { | |
251 | dev_err(&client->dev, "failed to register iio dev\n"); | |
252 | goto err_init; | |
253 | } | |
8bf62ec8 | 254 | |
8b054426 DB |
255 | return 0; |
256 | err_init: | |
257 | cm3323_disable(indio_dev); | |
258 | return ret; | |
259 | } | |
260 | ||
261 | static int cm3323_remove(struct i2c_client *client) | |
262 | { | |
263 | struct iio_dev *indio_dev = i2c_get_clientdata(client); | |
264 | ||
265 | iio_device_unregister(indio_dev); | |
266 | cm3323_disable(indio_dev); | |
267 | ||
268 | return 0; | |
269 | } | |
270 | ||
271 | static const struct i2c_device_id cm3323_id[] = { | |
272 | {"cm3323", 0}, | |
273 | {} | |
274 | }; | |
275 | MODULE_DEVICE_TABLE(i2c, cm3323_id); | |
276 | ||
277 | static struct i2c_driver cm3323_driver = { | |
278 | .driver = { | |
279 | .name = CM3323_DRV_NAME, | |
280 | }, | |
281 | .probe = cm3323_probe, | |
282 | .remove = cm3323_remove, | |
283 | .id_table = cm3323_id, | |
284 | }; | |
285 | ||
286 | module_i2c_driver(cm3323_driver); | |
287 | ||
288 | MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>"); | |
289 | MODULE_DESCRIPTION("Capella CM3323 Color Light Sensor driver"); | |
290 | MODULE_LICENSE("GPL v2"); |