This source file includes following definitions.
- ds2482_calculate_config
- ds2482_select_register
- ds2482_send_cmd
- ds2482_send_cmd_data
- ds2482_wait_1wire_idle
- ds2482_set_channel
- ds2482_w1_touch_bit
- ds2482_w1_triplet
- ds2482_w1_write_byte
- ds2482_w1_read_byte
- ds2482_w1_reset_bus
- ds2482_w1_set_pullup
- ds2482_probe
- ds2482_remove
1
2
3
4
5
6
7
8
9
10
11
12
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/i2c.h>
17 #include <linux/delay.h>
18 #include <asm/delay.h>
19
20 #include <linux/w1.h>
21
22
23
24
25
26
27
28
29
30
31
32 static int ds2482_active_pullup = 1;
33 module_param_named(active_pullup, ds2482_active_pullup, int, 0644);
34 MODULE_PARM_DESC(active_pullup, "Active pullup (apply to all buses): " \
35 "0-disable, 1-enable (default)");
36
37
38 static int extra_config;
39 module_param(extra_config, int, S_IRUGO | S_IWUSR);
40 MODULE_PARM_DESC(extra_config, "Extra Configuration settings 1=APU,2=PPM,3=SPU,8=1WS");
41
42
43
44
45
46
47
48 #define DS2482_CMD_RESET 0xF0
49 #define DS2482_CMD_SET_READ_PTR 0xE1
50 #define DS2482_CMD_CHANNEL_SELECT 0xC3
51 #define DS2482_CMD_WRITE_CONFIG 0xD2
52 #define DS2482_CMD_1WIRE_RESET 0xB4
53 #define DS2482_CMD_1WIRE_SINGLE_BIT 0x87
54 #define DS2482_CMD_1WIRE_WRITE_BYTE 0xA5
55 #define DS2482_CMD_1WIRE_READ_BYTE 0x96
56
57 #define DS2482_CMD_1WIRE_TRIPLET 0x78
58
59
60 #define DS2482_PTR_CODE_STATUS 0xF0
61 #define DS2482_PTR_CODE_DATA 0xE1
62 #define DS2482_PTR_CODE_CHANNEL 0xD2
63 #define DS2482_PTR_CODE_CONFIG 0xC3
64
65
66
67
68
69
70 #define DS2482_REG_CFG_1WS 0x08
71 #define DS2482_REG_CFG_SPU 0x04
72 #define DS2482_REG_CFG_PPM 0x02
73 #define DS2482_REG_CFG_APU 0x01
74
75
76
77
78
79
80
81 static const u8 ds2482_chan_wr[8] =
82 { 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87 };
83 static const u8 ds2482_chan_rd[8] =
84 { 0xB8, 0xB1, 0xAA, 0xA3, 0x9C, 0x95, 0x8E, 0x87 };
85
86
87
88
89
90 #define DS2482_REG_STS_DIR 0x80
91 #define DS2482_REG_STS_TSB 0x40
92 #define DS2482_REG_STS_SBR 0x20
93 #define DS2482_REG_STS_RST 0x10
94 #define DS2482_REG_STS_LL 0x08
95 #define DS2482_REG_STS_SD 0x04
96 #define DS2482_REG_STS_PPD 0x02
97 #define DS2482_REG_STS_1WB 0x01
98
99
100
101
102
103 struct ds2482_data;
104
105 struct ds2482_w1_chan {
106 struct ds2482_data *pdev;
107 u8 channel;
108 struct w1_bus_master w1_bm;
109 };
110
111 struct ds2482_data {
112 struct i2c_client *client;
113 struct mutex access_lock;
114
115
116 int w1_count;
117 struct ds2482_w1_chan w1_ch[8];
118
119
120 u8 channel;
121 u8 read_prt;
122 u8 reg_config;
123 };
124
125
126
127
128
129
130
131 static inline u8 ds2482_calculate_config(u8 conf)
132 {
133 conf |= extra_config;
134
135 if (ds2482_active_pullup)
136 conf |= DS2482_REG_CFG_APU;
137
138 return conf | ((~conf & 0x0f) << 4);
139 }
140
141
142
143
144
145
146
147
148 static inline int ds2482_select_register(struct ds2482_data *pdev, u8 read_ptr)
149 {
150 if (pdev->read_prt != read_ptr) {
151 if (i2c_smbus_write_byte_data(pdev->client,
152 DS2482_CMD_SET_READ_PTR,
153 read_ptr) < 0)
154 return -1;
155
156 pdev->read_prt = read_ptr;
157 }
158 return 0;
159 }
160
161
162
163
164
165
166
167
168
169 static inline int ds2482_send_cmd(struct ds2482_data *pdev, u8 cmd)
170 {
171 if (i2c_smbus_write_byte(pdev->client, cmd) < 0)
172 return -1;
173
174 pdev->read_prt = DS2482_PTR_CODE_STATUS;
175 return 0;
176 }
177
178
179
180
181
182
183
184
185
186
187
188 static inline int ds2482_send_cmd_data(struct ds2482_data *pdev,
189 u8 cmd, u8 byte)
190 {
191 if (i2c_smbus_write_byte_data(pdev->client, cmd, byte) < 0)
192 return -1;
193
194
195 pdev->read_prt = (cmd != DS2482_CMD_WRITE_CONFIG) ?
196 DS2482_PTR_CODE_STATUS : DS2482_PTR_CODE_CONFIG;
197 return 0;
198 }
199
200
201
202
203
204
205 #define DS2482_WAIT_IDLE_TIMEOUT 100
206
207
208
209
210
211
212
213 static int ds2482_wait_1wire_idle(struct ds2482_data *pdev)
214 {
215 int temp = -1;
216 int retries = 0;
217
218 if (!ds2482_select_register(pdev, DS2482_PTR_CODE_STATUS)) {
219 do {
220 temp = i2c_smbus_read_byte(pdev->client);
221 } while ((temp >= 0) && (temp & DS2482_REG_STS_1WB) &&
222 (++retries < DS2482_WAIT_IDLE_TIMEOUT));
223 }
224
225 if (retries >= DS2482_WAIT_IDLE_TIMEOUT)
226 pr_err("%s: timeout on channel %d\n",
227 __func__, pdev->channel);
228
229 return temp;
230 }
231
232
233
234
235
236
237
238
239
240 static int ds2482_set_channel(struct ds2482_data *pdev, u8 channel)
241 {
242 if (i2c_smbus_write_byte_data(pdev->client, DS2482_CMD_CHANNEL_SELECT,
243 ds2482_chan_wr[channel]) < 0)
244 return -1;
245
246 pdev->read_prt = DS2482_PTR_CODE_CHANNEL;
247 pdev->channel = -1;
248 if (i2c_smbus_read_byte(pdev->client) == ds2482_chan_rd[channel]) {
249 pdev->channel = channel;
250 return 0;
251 }
252 return -1;
253 }
254
255
256
257
258
259
260
261
262
263 static u8 ds2482_w1_touch_bit(void *data, u8 bit)
264 {
265 struct ds2482_w1_chan *pchan = data;
266 struct ds2482_data *pdev = pchan->pdev;
267 int status = -1;
268
269 mutex_lock(&pdev->access_lock);
270
271
272 ds2482_wait_1wire_idle(pdev);
273 if (pdev->w1_count > 1)
274 ds2482_set_channel(pdev, pchan->channel);
275
276
277 if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_SINGLE_BIT,
278 bit ? 0xFF : 0))
279 status = ds2482_wait_1wire_idle(pdev);
280
281 mutex_unlock(&pdev->access_lock);
282
283 return (status & DS2482_REG_STS_SBR) ? 1 : 0;
284 }
285
286
287
288
289
290
291
292
293
294
295 static u8 ds2482_w1_triplet(void *data, u8 dbit)
296 {
297 struct ds2482_w1_chan *pchan = data;
298 struct ds2482_data *pdev = pchan->pdev;
299 int status = (3 << 5);
300
301 mutex_lock(&pdev->access_lock);
302
303
304 ds2482_wait_1wire_idle(pdev);
305 if (pdev->w1_count > 1)
306 ds2482_set_channel(pdev, pchan->channel);
307
308
309 if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_TRIPLET,
310 dbit ? 0xFF : 0))
311 status = ds2482_wait_1wire_idle(pdev);
312
313 mutex_unlock(&pdev->access_lock);
314
315
316 return (status >> 5);
317 }
318
319
320
321
322
323
324
325 static void ds2482_w1_write_byte(void *data, u8 byte)
326 {
327 struct ds2482_w1_chan *pchan = data;
328 struct ds2482_data *pdev = pchan->pdev;
329
330 mutex_lock(&pdev->access_lock);
331
332
333 ds2482_wait_1wire_idle(pdev);
334 if (pdev->w1_count > 1)
335 ds2482_set_channel(pdev, pchan->channel);
336
337
338 ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_WRITE_BYTE, byte);
339
340 mutex_unlock(&pdev->access_lock);
341 }
342
343
344
345
346
347
348
349 static u8 ds2482_w1_read_byte(void *data)
350 {
351 struct ds2482_w1_chan *pchan = data;
352 struct ds2482_data *pdev = pchan->pdev;
353 int result;
354
355 mutex_lock(&pdev->access_lock);
356
357
358 ds2482_wait_1wire_idle(pdev);
359 if (pdev->w1_count > 1)
360 ds2482_set_channel(pdev, pchan->channel);
361
362
363 ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_READ_BYTE);
364
365
366 ds2482_wait_1wire_idle(pdev);
367
368
369 ds2482_select_register(pdev, DS2482_PTR_CODE_DATA);
370
371
372 result = i2c_smbus_read_byte(pdev->client);
373
374 mutex_unlock(&pdev->access_lock);
375
376 return result;
377 }
378
379
380
381
382
383
384
385
386 static u8 ds2482_w1_reset_bus(void *data)
387 {
388 struct ds2482_w1_chan *pchan = data;
389 struct ds2482_data *pdev = pchan->pdev;
390 int err;
391 u8 retval = 1;
392
393 mutex_lock(&pdev->access_lock);
394
395
396 ds2482_wait_1wire_idle(pdev);
397 if (pdev->w1_count > 1)
398 ds2482_set_channel(pdev, pchan->channel);
399
400
401 err = ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_RESET);
402 if (err >= 0) {
403
404 err = ds2482_wait_1wire_idle(pdev);
405 retval = !(err & DS2482_REG_STS_PPD);
406
407
408 if (err & DS2482_REG_STS_RST)
409 ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG,
410 ds2482_calculate_config(0x00));
411 }
412
413 mutex_unlock(&pdev->access_lock);
414
415 return retval;
416 }
417
418 static u8 ds2482_w1_set_pullup(void *data, int delay)
419 {
420 struct ds2482_w1_chan *pchan = data;
421 struct ds2482_data *pdev = pchan->pdev;
422 u8 retval = 1;
423
424
425
426
427
428 if (delay) {
429
430
431
432
433 ds2482_wait_1wire_idle(pdev);
434
435 retval = ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG,
436 ds2482_calculate_config(DS2482_REG_CFG_SPU |
437 DS2482_REG_CFG_APU));
438 ds2482_wait_1wire_idle(pdev);
439 }
440
441 return retval;
442 }
443
444
445 static int ds2482_probe(struct i2c_client *client,
446 const struct i2c_device_id *id)
447 {
448 struct ds2482_data *data;
449 int err = -ENODEV;
450 int temp1;
451 int idx;
452
453 if (!i2c_check_functionality(client->adapter,
454 I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
455 I2C_FUNC_SMBUS_BYTE))
456 return -ENODEV;
457
458 if (!(data = kzalloc(sizeof(struct ds2482_data), GFP_KERNEL))) {
459 err = -ENOMEM;
460 goto exit;
461 }
462
463 data->client = client;
464 i2c_set_clientdata(client, data);
465
466
467 if (ds2482_send_cmd(data, DS2482_CMD_RESET) < 0) {
468 dev_warn(&client->dev, "DS2482 reset failed.\n");
469 goto exit_free;
470 }
471
472
473 ndelay(525);
474
475
476 temp1 = i2c_smbus_read_byte(client);
477 if (temp1 != (DS2482_REG_STS_LL | DS2482_REG_STS_RST)) {
478 dev_warn(&client->dev, "DS2482 reset status "
479 "0x%02X - not a DS2482\n", temp1);
480 goto exit_free;
481 }
482
483
484 data->w1_count = 1;
485 if (ds2482_set_channel(data, 7) == 0)
486 data->w1_count = 8;
487
488
489 ds2482_send_cmd_data(data, DS2482_CMD_WRITE_CONFIG,
490 ds2482_calculate_config(0x00));
491
492 mutex_init(&data->access_lock);
493
494
495 for (idx = 0; idx < data->w1_count; idx++) {
496 data->w1_ch[idx].pdev = data;
497 data->w1_ch[idx].channel = idx;
498
499
500 data->w1_ch[idx].w1_bm.data = &data->w1_ch[idx];
501 data->w1_ch[idx].w1_bm.read_byte = ds2482_w1_read_byte;
502 data->w1_ch[idx].w1_bm.write_byte = ds2482_w1_write_byte;
503 data->w1_ch[idx].w1_bm.touch_bit = ds2482_w1_touch_bit;
504 data->w1_ch[idx].w1_bm.triplet = ds2482_w1_triplet;
505 data->w1_ch[idx].w1_bm.reset_bus = ds2482_w1_reset_bus;
506 data->w1_ch[idx].w1_bm.set_pullup = ds2482_w1_set_pullup;
507
508 err = w1_add_master_device(&data->w1_ch[idx].w1_bm);
509 if (err) {
510 data->w1_ch[idx].pdev = NULL;
511 goto exit_w1_remove;
512 }
513 }
514
515 return 0;
516
517 exit_w1_remove:
518 for (idx = 0; idx < data->w1_count; idx++) {
519 if (data->w1_ch[idx].pdev != NULL)
520 w1_remove_master_device(&data->w1_ch[idx].w1_bm);
521 }
522 exit_free:
523 kfree(data);
524 exit:
525 return err;
526 }
527
528 static int ds2482_remove(struct i2c_client *client)
529 {
530 struct ds2482_data *data = i2c_get_clientdata(client);
531 int idx;
532
533
534 for (idx = 0; idx < data->w1_count; idx++) {
535 if (data->w1_ch[idx].pdev != NULL)
536 w1_remove_master_device(&data->w1_ch[idx].w1_bm);
537 }
538
539
540 kfree(data);
541 return 0;
542 }
543
544
545
546
547 static const struct i2c_device_id ds2482_id[] = {
548 { "ds2482", 0 },
549 { }
550 };
551 MODULE_DEVICE_TABLE(i2c, ds2482_id);
552
553 static struct i2c_driver ds2482_driver = {
554 .driver = {
555 .name = "ds2482",
556 },
557 .probe = ds2482_probe,
558 .remove = ds2482_remove,
559 .id_table = ds2482_id,
560 };
561 module_i2c_driver(ds2482_driver);
562
563 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
564 MODULE_DESCRIPTION("DS2482 driver");
565
566 MODULE_LICENSE("GPL");