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