blob: fa13fa8c81af56006fc5e0be2226c0f7f104c0a2 [file] [log] [blame]
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +03001/**
2 * ds2482.c - provides i2c to w1-master bridge(s)
3 * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
4 *
5 * The DS2482 is a sensor chip made by Dallas Semiconductor (Maxim).
6 * It is a I2C to 1-wire bridge.
7 * There are two variations: -100 and -800, which have 1 or 8 1-wire ports.
8 * The complete datasheet can be obtained from MAXIM's website at:
9 * http://www.maxim-ic.com/quick_view2.cfm/qv_pk/4382
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
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>
Todd Poynorb1173ef2012-08-30 23:09:14 -070021#include <linux/gpio.h>
22#include <linux/platform_data/ds2482.h>
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +030023#include <asm/delay.h>
24
25#include "../w1.h"
26#include "../w1_int.h"
27
28/**
Mariusz Bialonczykf7049382016-03-29 18:41:38 +020029 * Allow the active pullup to be disabled, default is enabled.
30 *
31 * Note from the DS2482 datasheet:
32 * The APU bit controls whether an active pullup (controlled slew-rate
33 * transistor) or a passive pullup (Rwpu resistor) will be used to drive
34 * a 1-Wire line from low to high. When APU = 0, active pullup is disabled
35 * (resistor mode). Active Pullup should always be selected unless there is
36 * only a single slave on the 1-Wire line.
37 */
38static int ds2482_active_pullup = 1;
39module_param_named(active_pullup, ds2482_active_pullup, int, 0644);
40
41/**
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +030042 * The DS2482 registers - there are 3 registers that are addressed by a read
43 * pointer. The read pointer is set by the last command executed.
44 *
45 * To read the data, issue a register read for any address
46 */
47#define DS2482_CMD_RESET 0xF0 /* No param */
48#define DS2482_CMD_SET_READ_PTR 0xE1 /* Param: DS2482_PTR_CODE_xxx */
49#define DS2482_CMD_CHANNEL_SELECT 0xC3 /* Param: Channel byte - DS2482-800 only */
50#define DS2482_CMD_WRITE_CONFIG 0xD2 /* Param: Config byte */
51#define DS2482_CMD_1WIRE_RESET 0xB4 /* Param: None */
52#define DS2482_CMD_1WIRE_SINGLE_BIT 0x87 /* Param: Bit byte (bit7) */
53#define DS2482_CMD_1WIRE_WRITE_BYTE 0xA5 /* Param: Data byte */
54#define DS2482_CMD_1WIRE_READ_BYTE 0x96 /* Param: None */
55/* Note to read the byte, Set the ReadPtr to Data then read (any addr) */
56#define DS2482_CMD_1WIRE_TRIPLET 0x78 /* Param: Dir byte (bit7) */
57
58/* Values for DS2482_CMD_SET_READ_PTR */
59#define DS2482_PTR_CODE_STATUS 0xF0
60#define DS2482_PTR_CODE_DATA 0xE1
61#define DS2482_PTR_CODE_CHANNEL 0xD2 /* DS2482-800 only */
62#define DS2482_PTR_CODE_CONFIG 0xC3
63
64/**
65 * Configure Register bit definitions
66 * The top 4 bits always read 0.
67 * To write, the top nibble must be the 1's compl. of the low nibble.
68 */
Michael Arndt9c95bb62013-02-17 20:31:27 +010069#define DS2482_REG_CFG_1WS 0x08 /* 1-wire speed */
70#define DS2482_REG_CFG_SPU 0x04 /* strong pull-up */
71#define DS2482_REG_CFG_PPM 0x02 /* presence pulse masking */
72#define DS2482_REG_CFG_APU 0x01 /* active pull-up */
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +030073
74
75/**
76 * Write and verify codes for the CHANNEL_SELECT command (DS2482-800 only).
77 * To set the channel, write the value at the index of the channel.
78 * Read and compare against the corresponding value to verify the change.
79 */
80static const u8 ds2482_chan_wr[8] =
81 { 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87 };
82static const u8 ds2482_chan_rd[8] =
83 { 0xB8, 0xB1, 0xAA, 0xA3, 0x9C, 0x95, 0x8E, 0x87 };
84
85
86/**
87 * Status Register bit definitions (read only)
88 */
89#define DS2482_REG_STS_DIR 0x80
90#define DS2482_REG_STS_TSB 0x40
91#define DS2482_REG_STS_SBR 0x20
92#define DS2482_REG_STS_RST 0x10
93#define DS2482_REG_STS_LL 0x08
94#define DS2482_REG_STS_SD 0x04
95#define DS2482_REG_STS_PPD 0x02
96#define DS2482_REG_STS_1WB 0x01
97
98
Jean Delvare61c91f72008-07-16 19:30:07 +020099static int ds2482_probe(struct i2c_client *client,
100 const struct i2c_device_id *id);
Jean Delvare61c91f72008-07-16 19:30:07 +0200101static int ds2482_remove(struct i2c_client *client);
Todd Poynorb1173ef2012-08-30 23:09:14 -0700102static int ds2482_suspend(struct device *dev);
103static int ds2482_resume(struct device *dev);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300104
105/**
106 * Driver data (common to all clients)
107 */
Jean Delvare61c91f72008-07-16 19:30:07 +0200108static const struct i2c_device_id ds2482_id[] = {
109 { "ds2482", 0 },
110 { }
111};
Ludek Hlavacek0dcacd72015-05-27 00:37:59 +0200112MODULE_DEVICE_TABLE(i2c, ds2482_id);
Jean Delvare61c91f72008-07-16 19:30:07 +0200113
Todd Poynorb1173ef2012-08-30 23:09:14 -0700114static const struct dev_pm_ops ds2482_pm_ops = {
115 .suspend = ds2482_suspend,
116 .resume = ds2482_resume,
117};
118
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300119static struct i2c_driver ds2482_driver = {
120 .driver = {
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300121 .name = "ds2482",
Todd Poynorb1173ef2012-08-30 23:09:14 -0700122 .pm = &ds2482_pm_ops,
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300123 },
Jean Delvare61c91f72008-07-16 19:30:07 +0200124 .probe = ds2482_probe,
125 .remove = ds2482_remove,
126 .id_table = ds2482_id,
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300127};
128
129/*
130 * Client data (each client gets its own)
131 */
132
133struct ds2482_data;
134
135struct ds2482_w1_chan {
136 struct ds2482_data *pdev;
137 u8 channel;
138 struct w1_bus_master w1_bm;
139};
140
141struct ds2482_data {
Jean Delvare61c91f72008-07-16 19:30:07 +0200142 struct i2c_client *client;
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400143 struct mutex access_lock;
Todd Poynorb1173ef2012-08-30 23:09:14 -0700144 int slpz_gpio;
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300145
146 /* 1-wire interface(s) */
147 int w1_count; /* 1 or 8 */
148 struct ds2482_w1_chan w1_ch[8];
149
150 /* per-device values */
151 u8 channel;
152 u8 read_prt; /* see DS2482_PTR_CODE_xxx */
153 u8 reg_config;
154};
155
156
157/**
Michael Arndt9c95bb62013-02-17 20:31:27 +0100158 * Helper to calculate values for configuration register
159 * @param conf the raw config value
160 * @return the value w/ complements that can be written to register
161 */
162static inline u8 ds2482_calculate_config(u8 conf)
163{
Mariusz Bialonczykf7049382016-03-29 18:41:38 +0200164 if (ds2482_active_pullup)
165 conf |= DS2482_REG_CFG_APU;
166
Michael Arndt9c95bb62013-02-17 20:31:27 +0100167 return conf | ((~conf & 0x0f) << 4);
168}
169
170
171/**
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300172 * Sets the read pointer.
173 * @param pdev The ds2482 client pointer
174 * @param read_ptr see DS2482_PTR_CODE_xxx above
175 * @return -1 on failure, 0 on success
176 */
177static inline int ds2482_select_register(struct ds2482_data *pdev, u8 read_ptr)
178{
179 if (pdev->read_prt != read_ptr) {
Jean Delvare61c91f72008-07-16 19:30:07 +0200180 if (i2c_smbus_write_byte_data(pdev->client,
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300181 DS2482_CMD_SET_READ_PTR,
182 read_ptr) < 0)
183 return -1;
184
185 pdev->read_prt = read_ptr;
186 }
187 return 0;
188}
189
190/**
191 * Sends a command without a parameter
192 * @param pdev The ds2482 client pointer
193 * @param cmd DS2482_CMD_RESET,
194 * DS2482_CMD_1WIRE_RESET,
195 * DS2482_CMD_1WIRE_READ_BYTE
196 * @return -1 on failure, 0 on success
197 */
198static inline int ds2482_send_cmd(struct ds2482_data *pdev, u8 cmd)
199{
Jean Delvare61c91f72008-07-16 19:30:07 +0200200 if (i2c_smbus_write_byte(pdev->client, cmd) < 0)
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300201 return -1;
202
203 pdev->read_prt = DS2482_PTR_CODE_STATUS;
204 return 0;
205}
206
207/**
208 * Sends a command with a parameter
209 * @param pdev The ds2482 client pointer
210 * @param cmd DS2482_CMD_WRITE_CONFIG,
211 * DS2482_CMD_1WIRE_SINGLE_BIT,
212 * DS2482_CMD_1WIRE_WRITE_BYTE,
213 * DS2482_CMD_1WIRE_TRIPLET
214 * @param byte The data to send
215 * @return -1 on failure, 0 on success
216 */
217static inline int ds2482_send_cmd_data(struct ds2482_data *pdev,
218 u8 cmd, u8 byte)
219{
Jean Delvare61c91f72008-07-16 19:30:07 +0200220 if (i2c_smbus_write_byte_data(pdev->client, cmd, byte) < 0)
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300221 return -1;
222
223 /* all cmds leave in STATUS, except CONFIG */
224 pdev->read_prt = (cmd != DS2482_CMD_WRITE_CONFIG) ?
225 DS2482_PTR_CODE_STATUS : DS2482_PTR_CODE_CONFIG;
226 return 0;
227}
228
229
230/*
231 * 1-Wire interface code
232 */
233
234#define DS2482_WAIT_IDLE_TIMEOUT 100
235
236/**
237 * Waits until the 1-wire interface is idle (not busy)
238 *
239 * @param pdev Pointer to the device structure
240 * @return the last value read from status or -1 (failure)
241 */
242static int ds2482_wait_1wire_idle(struct ds2482_data *pdev)
243{
244 int temp = -1;
245 int retries = 0;
246
247 if (!ds2482_select_register(pdev, DS2482_PTR_CODE_STATUS)) {
248 do {
Jean Delvare61c91f72008-07-16 19:30:07 +0200249 temp = i2c_smbus_read_byte(pdev->client);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300250 } while ((temp >= 0) && (temp & DS2482_REG_STS_1WB) &&
Ben Gardnerb4786f12006-06-29 22:33:22 +0400251 (++retries < DS2482_WAIT_IDLE_TIMEOUT));
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300252 }
253
Roel Kluin67860732010-03-10 15:23:49 -0800254 if (retries >= DS2482_WAIT_IDLE_TIMEOUT)
Fjodor Schelichow1fda5692014-06-19 02:52:01 +0200255 pr_err("%s: timeout on channel %d\n",
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300256 __func__, pdev->channel);
257
258 return temp;
259}
260
261/**
262 * Selects a w1 channel.
263 * The 1-wire interface must be idle before calling this function.
264 *
265 * @param pdev The ds2482 client pointer
266 * @param channel 0-7
267 * @return -1 (failure) or 0 (success)
268 */
269static int ds2482_set_channel(struct ds2482_data *pdev, u8 channel)
270{
Jean Delvare61c91f72008-07-16 19:30:07 +0200271 if (i2c_smbus_write_byte_data(pdev->client, DS2482_CMD_CHANNEL_SELECT,
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300272 ds2482_chan_wr[channel]) < 0)
273 return -1;
274
275 pdev->read_prt = DS2482_PTR_CODE_CHANNEL;
276 pdev->channel = -1;
Jean Delvare61c91f72008-07-16 19:30:07 +0200277 if (i2c_smbus_read_byte(pdev->client) == ds2482_chan_rd[channel]) {
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300278 pdev->channel = channel;
279 return 0;
280 }
281 return -1;
282}
283
284
285/**
286 * Performs the touch-bit function, which writes a 0 or 1 and reads the level.
287 *
288 * @param data The ds2482 channel pointer
289 * @param bit The level to write: 0 or non-zero
290 * @return The level read: 0 or 1
291 */
292static u8 ds2482_w1_touch_bit(void *data, u8 bit)
293{
294 struct ds2482_w1_chan *pchan = data;
295 struct ds2482_data *pdev = pchan->pdev;
296 int status = -1;
297
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400298 mutex_lock(&pdev->access_lock);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300299
300 /* Select the channel */
301 ds2482_wait_1wire_idle(pdev);
302 if (pdev->w1_count > 1)
303 ds2482_set_channel(pdev, pchan->channel);
304
305 /* Send the touch command, wait until 1WB == 0, return the status */
306 if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_SINGLE_BIT,
307 bit ? 0xFF : 0))
308 status = ds2482_wait_1wire_idle(pdev);
309
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400310 mutex_unlock(&pdev->access_lock);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300311
312 return (status & DS2482_REG_STS_SBR) ? 1 : 0;
313}
314
315/**
316 * Performs the triplet function, which reads two bits and writes a bit.
317 * The bit written is determined by the two reads:
318 * 00 => dbit, 01 => 0, 10 => 1
319 *
320 * @param data The ds2482 channel pointer
321 * @param dbit The direction to choose if both branches are valid
322 * @return b0=read1 b1=read2 b3=bit written
323 */
324static u8 ds2482_w1_triplet(void *data, u8 dbit)
325{
326 struct ds2482_w1_chan *pchan = data;
327 struct ds2482_data *pdev = pchan->pdev;
328 int status = (3 << 5);
329
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400330 mutex_lock(&pdev->access_lock);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300331
332 /* Select the channel */
333 ds2482_wait_1wire_idle(pdev);
334 if (pdev->w1_count > 1)
335 ds2482_set_channel(pdev, pchan->channel);
336
337 /* Send the triplet command, wait until 1WB == 0, return the status */
338 if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_TRIPLET,
339 dbit ? 0xFF : 0))
340 status = ds2482_wait_1wire_idle(pdev);
341
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400342 mutex_unlock(&pdev->access_lock);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300343
344 /* Decode the status */
345 return (status >> 5);
346}
347
348/**
349 * Performs the write byte function.
350 *
351 * @param data The ds2482 channel pointer
352 * @param byte The value to write
353 */
354static void ds2482_w1_write_byte(void *data, u8 byte)
355{
356 struct ds2482_w1_chan *pchan = data;
357 struct ds2482_data *pdev = pchan->pdev;
358
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400359 mutex_lock(&pdev->access_lock);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300360
361 /* Select the channel */
362 ds2482_wait_1wire_idle(pdev);
363 if (pdev->w1_count > 1)
364 ds2482_set_channel(pdev, pchan->channel);
365
366 /* Send the write byte command */
367 ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_WRITE_BYTE, byte);
368
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400369 mutex_unlock(&pdev->access_lock);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300370}
371
372/**
373 * Performs the read byte function.
374 *
375 * @param data The ds2482 channel pointer
376 * @return The value read
377 */
378static u8 ds2482_w1_read_byte(void *data)
379{
380 struct ds2482_w1_chan *pchan = data;
381 struct ds2482_data *pdev = pchan->pdev;
382 int result;
383
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400384 mutex_lock(&pdev->access_lock);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300385
386 /* Select the channel */
387 ds2482_wait_1wire_idle(pdev);
388 if (pdev->w1_count > 1)
389 ds2482_set_channel(pdev, pchan->channel);
390
391 /* Send the read byte command */
392 ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_READ_BYTE);
393
394 /* Wait until 1WB == 0 */
395 ds2482_wait_1wire_idle(pdev);
396
397 /* Select the data register */
398 ds2482_select_register(pdev, DS2482_PTR_CODE_DATA);
399
400 /* Read the data byte */
Jean Delvare61c91f72008-07-16 19:30:07 +0200401 result = i2c_smbus_read_byte(pdev->client);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300402
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400403 mutex_unlock(&pdev->access_lock);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300404
405 return result;
406}
407
408
409/**
410 * Sends a reset on the 1-wire interface
411 *
412 * @param data The ds2482 channel pointer
413 * @return 0=Device present, 1=No device present or error
414 */
415static u8 ds2482_w1_reset_bus(void *data)
416{
417 struct ds2482_w1_chan *pchan = data;
418 struct ds2482_data *pdev = pchan->pdev;
419 int err;
420 u8 retval = 1;
421
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400422 mutex_lock(&pdev->access_lock);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300423
424 /* Select the channel */
425 ds2482_wait_1wire_idle(pdev);
426 if (pdev->w1_count > 1)
427 ds2482_set_channel(pdev, pchan->channel);
428
429 /* Send the reset command */
430 err = ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_RESET);
431 if (err >= 0) {
432 /* Wait until the reset is complete */
433 err = ds2482_wait_1wire_idle(pdev);
434 retval = !(err & DS2482_REG_STS_PPD);
435
436 /* If the chip did reset since detect, re-config it */
437 if (err & DS2482_REG_STS_RST)
438 ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG,
Michael Arndt9c95bb62013-02-17 20:31:27 +0100439 ds2482_calculate_config(0x00));
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300440 }
441
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400442 mutex_unlock(&pdev->access_lock);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300443
444 return retval;
445}
446
Michael Arndt9c95bb62013-02-17 20:31:27 +0100447static u8 ds2482_w1_set_pullup(void *data, int delay)
448{
449 struct ds2482_w1_chan *pchan = data;
450 struct ds2482_data *pdev = pchan->pdev;
451 u8 retval = 1;
452
453 /* if delay is non-zero activate the pullup,
454 * the strong pullup will be automatically deactivated
455 * by the master, so do not explicitly deactive it
456 */
457 if (delay) {
458 /* both waits are crucial, otherwise devices might not be
459 * powered long enough, causing e.g. a w1_therm sensor to
460 * provide wrong conversion results
461 */
462 ds2482_wait_1wire_idle(pdev);
463 /* note: it seems like both SPU and APU have to be set! */
464 retval = ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG,
465 ds2482_calculate_config(DS2482_REG_CFG_SPU |
466 DS2482_REG_CFG_APU));
467 ds2482_wait_1wire_idle(pdev);
468 }
469
470 return retval;
471}
472
Todd Poynorb1173ef2012-08-30 23:09:14 -0700473static int ds2482_suspend(struct device *dev)
474{
475 struct i2c_client *client = to_i2c_client(dev);
476 struct ds2482_data *data = i2c_get_clientdata(client);
477
478 if (data->slpz_gpio >= 0)
479 gpio_set_value(data->slpz_gpio, 0);
480 return 0;
481}
482
483static int ds2482_resume(struct device *dev)
484{
485 struct i2c_client *client = to_i2c_client(dev);
486 struct ds2482_data *data = i2c_get_clientdata(client);
487
488 if (data->slpz_gpio >= 0)
489 gpio_set_value(data->slpz_gpio, 1);
490 return 0;
491}
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300492
Jean Delvare61c91f72008-07-16 19:30:07 +0200493static int ds2482_probe(struct i2c_client *client,
494 const struct i2c_device_id *id)
495{
496 struct ds2482_data *data;
Todd Poynorb1173ef2012-08-30 23:09:14 -0700497 struct ds2482_platform_data *pdata;
Jean Delvare61c91f72008-07-16 19:30:07 +0200498 int err = -ENODEV;
499 int temp1;
500 int idx;
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300501
Jean Delvare0314b022009-10-04 22:53:41 +0200502 if (!i2c_check_functionality(client->adapter,
503 I2C_FUNC_SMBUS_WRITE_BYTE_DATA |
504 I2C_FUNC_SMBUS_BYTE))
505 return -ENODEV;
506
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300507 if (!(data = kzalloc(sizeof(struct ds2482_data), GFP_KERNEL))) {
508 err = -ENOMEM;
509 goto exit;
510 }
511
Jean Delvare61c91f72008-07-16 19:30:07 +0200512 data->client = client;
513 i2c_set_clientdata(client, data);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300514
515 /* Reset the device (sets the read_ptr to status) */
516 if (ds2482_send_cmd(data, DS2482_CMD_RESET) < 0) {
Jean Delvare61c91f72008-07-16 19:30:07 +0200517 dev_warn(&client->dev, "DS2482 reset failed.\n");
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300518 goto exit_free;
519 }
520
521 /* Sleep at least 525ns to allow the reset to complete */
522 ndelay(525);
523
524 /* Read the status byte - only reset bit and line should be set */
Jean Delvare61c91f72008-07-16 19:30:07 +0200525 temp1 = i2c_smbus_read_byte(client);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300526 if (temp1 != (DS2482_REG_STS_LL | DS2482_REG_STS_RST)) {
Jean Delvare61c91f72008-07-16 19:30:07 +0200527 dev_warn(&client->dev, "DS2482 reset status "
528 "0x%02X - not a DS2482\n", temp1);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300529 goto exit_free;
530 }
531
532 /* Detect the 8-port version */
533 data->w1_count = 1;
534 if (ds2482_set_channel(data, 7) == 0)
535 data->w1_count = 8;
536
537 /* Set all config items to 0 (off) */
Michael Arndt9c95bb62013-02-17 20:31:27 +0100538 ds2482_send_cmd_data(data, DS2482_CMD_WRITE_CONFIG,
539 ds2482_calculate_config(0x00));
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300540
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400541 mutex_init(&data->access_lock);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300542
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300543 /* Register 1-wire interface(s) */
544 for (idx = 0; idx < data->w1_count; idx++) {
545 data->w1_ch[idx].pdev = data;
546 data->w1_ch[idx].channel = idx;
547
548 /* Populate all the w1 bus master stuff */
549 data->w1_ch[idx].w1_bm.data = &data->w1_ch[idx];
550 data->w1_ch[idx].w1_bm.read_byte = ds2482_w1_read_byte;
551 data->w1_ch[idx].w1_bm.write_byte = ds2482_w1_write_byte;
552 data->w1_ch[idx].w1_bm.touch_bit = ds2482_w1_touch_bit;
553 data->w1_ch[idx].w1_bm.triplet = ds2482_w1_triplet;
554 data->w1_ch[idx].w1_bm.reset_bus = ds2482_w1_reset_bus;
Michael Arndt9c95bb62013-02-17 20:31:27 +0100555 data->w1_ch[idx].w1_bm.set_pullup = ds2482_w1_set_pullup;
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300556
557 err = w1_add_master_device(&data->w1_ch[idx].w1_bm);
558 if (err) {
559 data->w1_ch[idx].pdev = NULL;
560 goto exit_w1_remove;
561 }
562 }
563
Todd Poynorb1173ef2012-08-30 23:09:14 -0700564 pdata = client->dev.platform_data;
565 data->slpz_gpio = pdata ? pdata->slpz_gpio : -1;
566
567 if (data->slpz_gpio >= 0) {
568 err = gpio_request_one(data->slpz_gpio, GPIOF_OUT_INIT_HIGH,
569 "ds2482.slpz");
570 if (err < 0)
571 goto exit_w1_remove;
572 }
573
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300574 return 0;
575
576exit_w1_remove:
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300577 for (idx = 0; idx < data->w1_count; idx++) {
578 if (data->w1_ch[idx].pdev != NULL)
579 w1_remove_master_device(&data->w1_ch[idx].w1_bm);
580 }
581exit_free:
582 kfree(data);
583exit:
584 return err;
585}
586
Jean Delvare61c91f72008-07-16 19:30:07 +0200587static int ds2482_remove(struct i2c_client *client)
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300588{
589 struct ds2482_data *data = i2c_get_clientdata(client);
Jean Delvare61c91f72008-07-16 19:30:07 +0200590 int idx;
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300591
592 /* Unregister the 1-wire bridge(s) */
593 for (idx = 0; idx < data->w1_count; idx++) {
594 if (data->w1_ch[idx].pdev != NULL)
595 w1_remove_master_device(&data->w1_ch[idx].w1_bm);
596 }
597
Todd Poynorb1173ef2012-08-30 23:09:14 -0700598 if (data->slpz_gpio >= 0) {
599 gpio_set_value(data->slpz_gpio, 0);
600 gpio_free(data->slpz_gpio);
601 }
602
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300603 /* Free the memory */
604 kfree(data);
605 return 0;
606}
607
Wei Yongjundbfd5cc2012-10-08 22:06:07 +0800608module_i2c_driver(ds2482_driver);
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300609
Mariusz Bialonczykf7049382016-03-29 18:41:38 +0200610MODULE_PARM_DESC(active_pullup, "Active pullup (apply to all buses): " \
611 "0-disable, 1-enable (default)");
Evgeniy Polyakovbaf12ae2005-12-06 13:38:28 +0300612MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
613MODULE_DESCRIPTION("DS2482 driver");
614MODULE_LICENSE("GPL");