Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 1 | /** |
| 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> |
| 21 | #include <asm/delay.h> |
| 22 | |
| 23 | #include "../w1.h" |
| 24 | #include "../w1_int.h" |
| 25 | |
| 26 | /** |
Mariusz Bialonczyk | f704938 | 2016-03-29 18:41:38 +0200 | [diff] [blame] | 27 | * Allow the active pullup to be disabled, default is enabled. |
| 28 | * |
| 29 | * Note from the DS2482 datasheet: |
| 30 | * The APU bit controls whether an active pullup (controlled slew-rate |
| 31 | * transistor) or a passive pullup (Rwpu resistor) will be used to drive |
| 32 | * a 1-Wire line from low to high. When APU = 0, active pullup is disabled |
| 33 | * (resistor mode). Active Pullup should always be selected unless there is |
| 34 | * only a single slave on the 1-Wire line. |
| 35 | */ |
| 36 | static int ds2482_active_pullup = 1; |
| 37 | module_param_named(active_pullup, ds2482_active_pullup, int, 0644); |
| 38 | |
| 39 | /** |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 40 | * The DS2482 registers - there are 3 registers that are addressed by a read |
| 41 | * pointer. The read pointer is set by the last command executed. |
| 42 | * |
| 43 | * To read the data, issue a register read for any address |
| 44 | */ |
| 45 | #define DS2482_CMD_RESET 0xF0 /* No param */ |
| 46 | #define DS2482_CMD_SET_READ_PTR 0xE1 /* Param: DS2482_PTR_CODE_xxx */ |
| 47 | #define DS2482_CMD_CHANNEL_SELECT 0xC3 /* Param: Channel byte - DS2482-800 only */ |
| 48 | #define DS2482_CMD_WRITE_CONFIG 0xD2 /* Param: Config byte */ |
| 49 | #define DS2482_CMD_1WIRE_RESET 0xB4 /* Param: None */ |
| 50 | #define DS2482_CMD_1WIRE_SINGLE_BIT 0x87 /* Param: Bit byte (bit7) */ |
| 51 | #define DS2482_CMD_1WIRE_WRITE_BYTE 0xA5 /* Param: Data byte */ |
| 52 | #define DS2482_CMD_1WIRE_READ_BYTE 0x96 /* Param: None */ |
| 53 | /* Note to read the byte, Set the ReadPtr to Data then read (any addr) */ |
| 54 | #define DS2482_CMD_1WIRE_TRIPLET 0x78 /* Param: Dir byte (bit7) */ |
| 55 | |
| 56 | /* Values for DS2482_CMD_SET_READ_PTR */ |
| 57 | #define DS2482_PTR_CODE_STATUS 0xF0 |
| 58 | #define DS2482_PTR_CODE_DATA 0xE1 |
| 59 | #define DS2482_PTR_CODE_CHANNEL 0xD2 /* DS2482-800 only */ |
| 60 | #define DS2482_PTR_CODE_CONFIG 0xC3 |
| 61 | |
| 62 | /** |
| 63 | * Configure Register bit definitions |
| 64 | * The top 4 bits always read 0. |
| 65 | * To write, the top nibble must be the 1's compl. of the low nibble. |
| 66 | */ |
Michael Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 67 | #define DS2482_REG_CFG_1WS 0x08 /* 1-wire speed */ |
| 68 | #define DS2482_REG_CFG_SPU 0x04 /* strong pull-up */ |
| 69 | #define DS2482_REG_CFG_PPM 0x02 /* presence pulse masking */ |
| 70 | #define DS2482_REG_CFG_APU 0x01 /* active pull-up */ |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 71 | |
| 72 | |
| 73 | /** |
| 74 | * Write and verify codes for the CHANNEL_SELECT command (DS2482-800 only). |
| 75 | * To set the channel, write the value at the index of the channel. |
| 76 | * Read and compare against the corresponding value to verify the change. |
| 77 | */ |
| 78 | static const u8 ds2482_chan_wr[8] = |
| 79 | { 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87 }; |
| 80 | static const u8 ds2482_chan_rd[8] = |
| 81 | { 0xB8, 0xB1, 0xAA, 0xA3, 0x9C, 0x95, 0x8E, 0x87 }; |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * Status Register bit definitions (read only) |
| 86 | */ |
| 87 | #define DS2482_REG_STS_DIR 0x80 |
| 88 | #define DS2482_REG_STS_TSB 0x40 |
| 89 | #define DS2482_REG_STS_SBR 0x20 |
| 90 | #define DS2482_REG_STS_RST 0x10 |
| 91 | #define DS2482_REG_STS_LL 0x08 |
| 92 | #define DS2482_REG_STS_SD 0x04 |
| 93 | #define DS2482_REG_STS_PPD 0x02 |
| 94 | #define DS2482_REG_STS_1WB 0x01 |
| 95 | |
| 96 | |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 97 | static int ds2482_probe(struct i2c_client *client, |
| 98 | const struct i2c_device_id *id); |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 99 | static int ds2482_remove(struct i2c_client *client); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 100 | |
| 101 | |
| 102 | /** |
| 103 | * Driver data (common to all clients) |
| 104 | */ |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 105 | static const struct i2c_device_id ds2482_id[] = { |
| 106 | { "ds2482", 0 }, |
| 107 | { } |
| 108 | }; |
Ludek Hlavacek | 0dcacd7 | 2015-05-27 00:37:59 +0200 | [diff] [blame] | 109 | MODULE_DEVICE_TABLE(i2c, ds2482_id); |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 110 | |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 111 | static struct i2c_driver ds2482_driver = { |
| 112 | .driver = { |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 113 | .name = "ds2482", |
| 114 | }, |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 115 | .probe = ds2482_probe, |
| 116 | .remove = ds2482_remove, |
| 117 | .id_table = ds2482_id, |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | /* |
| 121 | * Client data (each client gets its own) |
| 122 | */ |
| 123 | |
| 124 | struct ds2482_data; |
| 125 | |
| 126 | struct ds2482_w1_chan { |
| 127 | struct ds2482_data *pdev; |
| 128 | u8 channel; |
| 129 | struct w1_bus_master w1_bm; |
| 130 | }; |
| 131 | |
| 132 | struct ds2482_data { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 133 | struct i2c_client *client; |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 134 | struct mutex access_lock; |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 135 | |
| 136 | /* 1-wire interface(s) */ |
| 137 | int w1_count; /* 1 or 8 */ |
| 138 | struct ds2482_w1_chan w1_ch[8]; |
| 139 | |
| 140 | /* per-device values */ |
| 141 | u8 channel; |
| 142 | u8 read_prt; /* see DS2482_PTR_CODE_xxx */ |
| 143 | u8 reg_config; |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | /** |
Michael Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 148 | * Helper to calculate values for configuration register |
| 149 | * @param conf the raw config value |
| 150 | * @return the value w/ complements that can be written to register |
| 151 | */ |
| 152 | static inline u8 ds2482_calculate_config(u8 conf) |
| 153 | { |
Mariusz Bialonczyk | f704938 | 2016-03-29 18:41:38 +0200 | [diff] [blame] | 154 | if (ds2482_active_pullup) |
| 155 | conf |= DS2482_REG_CFG_APU; |
| 156 | |
Michael Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 157 | return conf | ((~conf & 0x0f) << 4); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | /** |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 162 | * Sets the read pointer. |
| 163 | * @param pdev The ds2482 client pointer |
| 164 | * @param read_ptr see DS2482_PTR_CODE_xxx above |
| 165 | * @return -1 on failure, 0 on success |
| 166 | */ |
| 167 | static inline int ds2482_select_register(struct ds2482_data *pdev, u8 read_ptr) |
| 168 | { |
| 169 | if (pdev->read_prt != read_ptr) { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 170 | if (i2c_smbus_write_byte_data(pdev->client, |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 171 | DS2482_CMD_SET_READ_PTR, |
| 172 | read_ptr) < 0) |
| 173 | return -1; |
| 174 | |
| 175 | pdev->read_prt = read_ptr; |
| 176 | } |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Sends a command without a parameter |
| 182 | * @param pdev The ds2482 client pointer |
| 183 | * @param cmd DS2482_CMD_RESET, |
| 184 | * DS2482_CMD_1WIRE_RESET, |
| 185 | * DS2482_CMD_1WIRE_READ_BYTE |
| 186 | * @return -1 on failure, 0 on success |
| 187 | */ |
| 188 | static inline int ds2482_send_cmd(struct ds2482_data *pdev, u8 cmd) |
| 189 | { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 190 | if (i2c_smbus_write_byte(pdev->client, cmd) < 0) |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 191 | return -1; |
| 192 | |
| 193 | pdev->read_prt = DS2482_PTR_CODE_STATUS; |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Sends a command with a parameter |
| 199 | * @param pdev The ds2482 client pointer |
| 200 | * @param cmd DS2482_CMD_WRITE_CONFIG, |
| 201 | * DS2482_CMD_1WIRE_SINGLE_BIT, |
| 202 | * DS2482_CMD_1WIRE_WRITE_BYTE, |
| 203 | * DS2482_CMD_1WIRE_TRIPLET |
| 204 | * @param byte The data to send |
| 205 | * @return -1 on failure, 0 on success |
| 206 | */ |
| 207 | static inline int ds2482_send_cmd_data(struct ds2482_data *pdev, |
| 208 | u8 cmd, u8 byte) |
| 209 | { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 210 | if (i2c_smbus_write_byte_data(pdev->client, cmd, byte) < 0) |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 211 | return -1; |
| 212 | |
| 213 | /* all cmds leave in STATUS, except CONFIG */ |
| 214 | pdev->read_prt = (cmd != DS2482_CMD_WRITE_CONFIG) ? |
| 215 | DS2482_PTR_CODE_STATUS : DS2482_PTR_CODE_CONFIG; |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | /* |
| 221 | * 1-Wire interface code |
| 222 | */ |
| 223 | |
| 224 | #define DS2482_WAIT_IDLE_TIMEOUT 100 |
| 225 | |
| 226 | /** |
| 227 | * Waits until the 1-wire interface is idle (not busy) |
| 228 | * |
| 229 | * @param pdev Pointer to the device structure |
| 230 | * @return the last value read from status or -1 (failure) |
| 231 | */ |
| 232 | static int ds2482_wait_1wire_idle(struct ds2482_data *pdev) |
| 233 | { |
| 234 | int temp = -1; |
| 235 | int retries = 0; |
| 236 | |
| 237 | if (!ds2482_select_register(pdev, DS2482_PTR_CODE_STATUS)) { |
| 238 | do { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 239 | temp = i2c_smbus_read_byte(pdev->client); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 240 | } while ((temp >= 0) && (temp & DS2482_REG_STS_1WB) && |
Ben Gardner | b4786f1 | 2006-06-29 22:33:22 +0400 | [diff] [blame] | 241 | (++retries < DS2482_WAIT_IDLE_TIMEOUT)); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 242 | } |
| 243 | |
Roel Kluin | 6786073 | 2010-03-10 15:23:49 -0800 | [diff] [blame] | 244 | if (retries >= DS2482_WAIT_IDLE_TIMEOUT) |
Fjodor Schelichow | 1fda569 | 2014-06-19 02:52:01 +0200 | [diff] [blame] | 245 | pr_err("%s: timeout on channel %d\n", |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 246 | __func__, pdev->channel); |
| 247 | |
| 248 | return temp; |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Selects a w1 channel. |
| 253 | * The 1-wire interface must be idle before calling this function. |
| 254 | * |
| 255 | * @param pdev The ds2482 client pointer |
| 256 | * @param channel 0-7 |
| 257 | * @return -1 (failure) or 0 (success) |
| 258 | */ |
| 259 | static int ds2482_set_channel(struct ds2482_data *pdev, u8 channel) |
| 260 | { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 261 | if (i2c_smbus_write_byte_data(pdev->client, DS2482_CMD_CHANNEL_SELECT, |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 262 | ds2482_chan_wr[channel]) < 0) |
| 263 | return -1; |
| 264 | |
| 265 | pdev->read_prt = DS2482_PTR_CODE_CHANNEL; |
| 266 | pdev->channel = -1; |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 267 | if (i2c_smbus_read_byte(pdev->client) == ds2482_chan_rd[channel]) { |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 268 | pdev->channel = channel; |
| 269 | return 0; |
| 270 | } |
| 271 | return -1; |
| 272 | } |
| 273 | |
| 274 | |
| 275 | /** |
| 276 | * Performs the touch-bit function, which writes a 0 or 1 and reads the level. |
| 277 | * |
| 278 | * @param data The ds2482 channel pointer |
| 279 | * @param bit The level to write: 0 or non-zero |
| 280 | * @return The level read: 0 or 1 |
| 281 | */ |
| 282 | static u8 ds2482_w1_touch_bit(void *data, u8 bit) |
| 283 | { |
| 284 | struct ds2482_w1_chan *pchan = data; |
| 285 | struct ds2482_data *pdev = pchan->pdev; |
| 286 | int status = -1; |
| 287 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 288 | mutex_lock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 289 | |
| 290 | /* Select the channel */ |
| 291 | ds2482_wait_1wire_idle(pdev); |
| 292 | if (pdev->w1_count > 1) |
| 293 | ds2482_set_channel(pdev, pchan->channel); |
| 294 | |
| 295 | /* Send the touch command, wait until 1WB == 0, return the status */ |
| 296 | if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_SINGLE_BIT, |
| 297 | bit ? 0xFF : 0)) |
| 298 | status = ds2482_wait_1wire_idle(pdev); |
| 299 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 300 | mutex_unlock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 301 | |
| 302 | return (status & DS2482_REG_STS_SBR) ? 1 : 0; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Performs the triplet function, which reads two bits and writes a bit. |
| 307 | * The bit written is determined by the two reads: |
| 308 | * 00 => dbit, 01 => 0, 10 => 1 |
| 309 | * |
| 310 | * @param data The ds2482 channel pointer |
| 311 | * @param dbit The direction to choose if both branches are valid |
| 312 | * @return b0=read1 b1=read2 b3=bit written |
| 313 | */ |
| 314 | static u8 ds2482_w1_triplet(void *data, u8 dbit) |
| 315 | { |
| 316 | struct ds2482_w1_chan *pchan = data; |
| 317 | struct ds2482_data *pdev = pchan->pdev; |
| 318 | int status = (3 << 5); |
| 319 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 320 | mutex_lock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 321 | |
| 322 | /* Select the channel */ |
| 323 | ds2482_wait_1wire_idle(pdev); |
| 324 | if (pdev->w1_count > 1) |
| 325 | ds2482_set_channel(pdev, pchan->channel); |
| 326 | |
| 327 | /* Send the triplet command, wait until 1WB == 0, return the status */ |
| 328 | if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_TRIPLET, |
| 329 | dbit ? 0xFF : 0)) |
| 330 | status = ds2482_wait_1wire_idle(pdev); |
| 331 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 332 | mutex_unlock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 333 | |
| 334 | /* Decode the status */ |
| 335 | return (status >> 5); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Performs the write byte function. |
| 340 | * |
| 341 | * @param data The ds2482 channel pointer |
| 342 | * @param byte The value to write |
| 343 | */ |
| 344 | static void ds2482_w1_write_byte(void *data, u8 byte) |
| 345 | { |
| 346 | struct ds2482_w1_chan *pchan = data; |
| 347 | struct ds2482_data *pdev = pchan->pdev; |
| 348 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 349 | mutex_lock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 350 | |
| 351 | /* Select the channel */ |
| 352 | ds2482_wait_1wire_idle(pdev); |
| 353 | if (pdev->w1_count > 1) |
| 354 | ds2482_set_channel(pdev, pchan->channel); |
| 355 | |
| 356 | /* Send the write byte command */ |
| 357 | ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_WRITE_BYTE, byte); |
| 358 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 359 | mutex_unlock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Performs the read byte function. |
| 364 | * |
| 365 | * @param data The ds2482 channel pointer |
| 366 | * @return The value read |
| 367 | */ |
| 368 | static u8 ds2482_w1_read_byte(void *data) |
| 369 | { |
| 370 | struct ds2482_w1_chan *pchan = data; |
| 371 | struct ds2482_data *pdev = pchan->pdev; |
| 372 | int result; |
| 373 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 374 | mutex_lock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 375 | |
| 376 | /* Select the channel */ |
| 377 | ds2482_wait_1wire_idle(pdev); |
| 378 | if (pdev->w1_count > 1) |
| 379 | ds2482_set_channel(pdev, pchan->channel); |
| 380 | |
| 381 | /* Send the read byte command */ |
| 382 | ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_READ_BYTE); |
| 383 | |
| 384 | /* Wait until 1WB == 0 */ |
| 385 | ds2482_wait_1wire_idle(pdev); |
| 386 | |
| 387 | /* Select the data register */ |
| 388 | ds2482_select_register(pdev, DS2482_PTR_CODE_DATA); |
| 389 | |
| 390 | /* Read the data byte */ |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 391 | result = i2c_smbus_read_byte(pdev->client); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 392 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 393 | mutex_unlock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 394 | |
| 395 | return result; |
| 396 | } |
| 397 | |
| 398 | |
| 399 | /** |
| 400 | * Sends a reset on the 1-wire interface |
| 401 | * |
| 402 | * @param data The ds2482 channel pointer |
| 403 | * @return 0=Device present, 1=No device present or error |
| 404 | */ |
| 405 | static u8 ds2482_w1_reset_bus(void *data) |
| 406 | { |
| 407 | struct ds2482_w1_chan *pchan = data; |
| 408 | struct ds2482_data *pdev = pchan->pdev; |
| 409 | int err; |
| 410 | u8 retval = 1; |
| 411 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 412 | mutex_lock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 413 | |
| 414 | /* Select the channel */ |
| 415 | ds2482_wait_1wire_idle(pdev); |
| 416 | if (pdev->w1_count > 1) |
| 417 | ds2482_set_channel(pdev, pchan->channel); |
| 418 | |
| 419 | /* Send the reset command */ |
| 420 | err = ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_RESET); |
| 421 | if (err >= 0) { |
| 422 | /* Wait until the reset is complete */ |
| 423 | err = ds2482_wait_1wire_idle(pdev); |
| 424 | retval = !(err & DS2482_REG_STS_PPD); |
| 425 | |
| 426 | /* If the chip did reset since detect, re-config it */ |
| 427 | if (err & DS2482_REG_STS_RST) |
| 428 | ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG, |
Michael Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 429 | ds2482_calculate_config(0x00)); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 430 | } |
| 431 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 432 | mutex_unlock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 433 | |
| 434 | return retval; |
| 435 | } |
| 436 | |
Michael Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 437 | static u8 ds2482_w1_set_pullup(void *data, int delay) |
| 438 | { |
| 439 | struct ds2482_w1_chan *pchan = data; |
| 440 | struct ds2482_data *pdev = pchan->pdev; |
| 441 | u8 retval = 1; |
| 442 | |
| 443 | /* if delay is non-zero activate the pullup, |
| 444 | * the strong pullup will be automatically deactivated |
| 445 | * by the master, so do not explicitly deactive it |
| 446 | */ |
| 447 | if (delay) { |
| 448 | /* both waits are crucial, otherwise devices might not be |
| 449 | * powered long enough, causing e.g. a w1_therm sensor to |
| 450 | * provide wrong conversion results |
| 451 | */ |
| 452 | ds2482_wait_1wire_idle(pdev); |
| 453 | /* note: it seems like both SPU and APU have to be set! */ |
| 454 | retval = ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG, |
| 455 | ds2482_calculate_config(DS2482_REG_CFG_SPU | |
| 456 | DS2482_REG_CFG_APU)); |
| 457 | ds2482_wait_1wire_idle(pdev); |
| 458 | } |
| 459 | |
| 460 | return retval; |
| 461 | } |
| 462 | |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 463 | |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 464 | static int ds2482_probe(struct i2c_client *client, |
| 465 | const struct i2c_device_id *id) |
| 466 | { |
| 467 | struct ds2482_data *data; |
| 468 | int err = -ENODEV; |
| 469 | int temp1; |
| 470 | int idx; |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 471 | |
Jean Delvare | 0314b02 | 2009-10-04 22:53:41 +0200 | [diff] [blame] | 472 | if (!i2c_check_functionality(client->adapter, |
| 473 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA | |
| 474 | I2C_FUNC_SMBUS_BYTE)) |
| 475 | return -ENODEV; |
| 476 | |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 477 | if (!(data = kzalloc(sizeof(struct ds2482_data), GFP_KERNEL))) { |
| 478 | err = -ENOMEM; |
| 479 | goto exit; |
| 480 | } |
| 481 | |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 482 | data->client = client; |
| 483 | i2c_set_clientdata(client, data); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 484 | |
| 485 | /* Reset the device (sets the read_ptr to status) */ |
| 486 | if (ds2482_send_cmd(data, DS2482_CMD_RESET) < 0) { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 487 | dev_warn(&client->dev, "DS2482 reset failed.\n"); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 488 | goto exit_free; |
| 489 | } |
| 490 | |
| 491 | /* Sleep at least 525ns to allow the reset to complete */ |
| 492 | ndelay(525); |
| 493 | |
| 494 | /* Read the status byte - only reset bit and line should be set */ |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 495 | temp1 = i2c_smbus_read_byte(client); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 496 | if (temp1 != (DS2482_REG_STS_LL | DS2482_REG_STS_RST)) { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 497 | dev_warn(&client->dev, "DS2482 reset status " |
| 498 | "0x%02X - not a DS2482\n", temp1); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 499 | goto exit_free; |
| 500 | } |
| 501 | |
| 502 | /* Detect the 8-port version */ |
| 503 | data->w1_count = 1; |
| 504 | if (ds2482_set_channel(data, 7) == 0) |
| 505 | data->w1_count = 8; |
| 506 | |
| 507 | /* Set all config items to 0 (off) */ |
Michael Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 508 | ds2482_send_cmd_data(data, DS2482_CMD_WRITE_CONFIG, |
| 509 | ds2482_calculate_config(0x00)); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 510 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 511 | mutex_init(&data->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 512 | |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 513 | /* Register 1-wire interface(s) */ |
| 514 | for (idx = 0; idx < data->w1_count; idx++) { |
| 515 | data->w1_ch[idx].pdev = data; |
| 516 | data->w1_ch[idx].channel = idx; |
| 517 | |
| 518 | /* Populate all the w1 bus master stuff */ |
| 519 | data->w1_ch[idx].w1_bm.data = &data->w1_ch[idx]; |
| 520 | data->w1_ch[idx].w1_bm.read_byte = ds2482_w1_read_byte; |
| 521 | data->w1_ch[idx].w1_bm.write_byte = ds2482_w1_write_byte; |
| 522 | data->w1_ch[idx].w1_bm.touch_bit = ds2482_w1_touch_bit; |
| 523 | data->w1_ch[idx].w1_bm.triplet = ds2482_w1_triplet; |
| 524 | data->w1_ch[idx].w1_bm.reset_bus = ds2482_w1_reset_bus; |
Michael Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 525 | data->w1_ch[idx].w1_bm.set_pullup = ds2482_w1_set_pullup; |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 526 | |
| 527 | err = w1_add_master_device(&data->w1_ch[idx].w1_bm); |
| 528 | if (err) { |
| 529 | data->w1_ch[idx].pdev = NULL; |
| 530 | goto exit_w1_remove; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return 0; |
| 535 | |
| 536 | exit_w1_remove: |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 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 | exit_free: |
| 542 | kfree(data); |
| 543 | exit: |
| 544 | return err; |
| 545 | } |
| 546 | |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 547 | static int ds2482_remove(struct i2c_client *client) |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 548 | { |
| 549 | struct ds2482_data *data = i2c_get_clientdata(client); |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 550 | int idx; |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 551 | |
| 552 | /* Unregister the 1-wire bridge(s) */ |
| 553 | for (idx = 0; idx < data->w1_count; idx++) { |
| 554 | if (data->w1_ch[idx].pdev != NULL) |
| 555 | w1_remove_master_device(&data->w1_ch[idx].w1_bm); |
| 556 | } |
| 557 | |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 558 | /* Free the memory */ |
| 559 | kfree(data); |
| 560 | return 0; |
| 561 | } |
| 562 | |
Wei Yongjun | dbfd5cc | 2012-10-08 22:06:07 +0800 | [diff] [blame] | 563 | module_i2c_driver(ds2482_driver); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 564 | |
Mariusz Bialonczyk | f704938 | 2016-03-29 18:41:38 +0200 | [diff] [blame] | 565 | MODULE_PARM_DESC(active_pullup, "Active pullup (apply to all buses): " \ |
| 566 | "0-disable, 1-enable (default)"); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 567 | MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>"); |
| 568 | MODULE_DESCRIPTION("DS2482 driver"); |
| 569 | MODULE_LICENSE("GPL"); |