Thomas Gleixner | b886d83 | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 2 | /** |
| 3 | * ds2482.c - provides i2c to w1-master bridge(s) |
| 4 | * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com> |
| 5 | * |
| 6 | * The DS2482 is a sensor chip made by Dallas Semiconductor (Maxim). |
| 7 | * It is a I2C to 1-wire bridge. |
| 8 | * There are two variations: -100 and -800, which have 1 or 8 1-wire ports. |
| 9 | * The complete datasheet can be obtained from MAXIM's website at: |
| 10 | * http://www.maxim-ic.com/quick_view2.cfm/qv_pk/4382 |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 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 | |
Andrew F. Davis | de0d6db | 2017-06-05 08:52:08 -0500 | [diff] [blame] | 20 | #include <linux/w1.h> |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 21 | |
| 22 | /** |
Mariusz Bialonczyk | f704938 | 2016-03-29 18:41:38 +0200 | [diff] [blame] | 23 | * Allow the active pullup to be disabled, default is enabled. |
| 24 | * |
| 25 | * Note from the DS2482 datasheet: |
| 26 | * The APU bit controls whether an active pullup (controlled slew-rate |
| 27 | * transistor) or a passive pullup (Rwpu resistor) will be used to drive |
| 28 | * a 1-Wire line from low to high. When APU = 0, active pullup is disabled |
| 29 | * (resistor mode). Active Pullup should always be selected unless there is |
| 30 | * only a single slave on the 1-Wire line. |
| 31 | */ |
| 32 | static int ds2482_active_pullup = 1; |
| 33 | module_param_named(active_pullup, ds2482_active_pullup, int, 0644); |
Andrew F. Davis | 50fa295 | 2017-05-16 15:02:12 -0500 | [diff] [blame] | 34 | MODULE_PARM_DESC(active_pullup, "Active pullup (apply to all buses): " \ |
| 35 | "0-disable, 1-enable (default)"); |
Mariusz Bialonczyk | f704938 | 2016-03-29 18:41:38 +0200 | [diff] [blame] | 36 | |
Mariusz Bialonczyk | 5cb27d3 | 2019-03-04 12:23:36 +0100 | [diff] [blame] | 37 | /* extra configurations - e.g. 1WS */ |
| 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 | |
Mariusz Bialonczyk | f704938 | 2016-03-29 18:41:38 +0200 | [diff] [blame] | 42 | /** |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 43 | * The DS2482 registers - there are 3 registers that are addressed by a read |
| 44 | * pointer. The read pointer is set by the last command executed. |
| 45 | * |
| 46 | * To read the data, issue a register read for any address |
| 47 | */ |
| 48 | #define DS2482_CMD_RESET 0xF0 /* No param */ |
| 49 | #define DS2482_CMD_SET_READ_PTR 0xE1 /* Param: DS2482_PTR_CODE_xxx */ |
| 50 | #define DS2482_CMD_CHANNEL_SELECT 0xC3 /* Param: Channel byte - DS2482-800 only */ |
| 51 | #define DS2482_CMD_WRITE_CONFIG 0xD2 /* Param: Config byte */ |
| 52 | #define DS2482_CMD_1WIRE_RESET 0xB4 /* Param: None */ |
| 53 | #define DS2482_CMD_1WIRE_SINGLE_BIT 0x87 /* Param: Bit byte (bit7) */ |
| 54 | #define DS2482_CMD_1WIRE_WRITE_BYTE 0xA5 /* Param: Data byte */ |
| 55 | #define DS2482_CMD_1WIRE_READ_BYTE 0x96 /* Param: None */ |
| 56 | /* Note to read the byte, Set the ReadPtr to Data then read (any addr) */ |
| 57 | #define DS2482_CMD_1WIRE_TRIPLET 0x78 /* Param: Dir byte (bit7) */ |
| 58 | |
| 59 | /* Values for DS2482_CMD_SET_READ_PTR */ |
| 60 | #define DS2482_PTR_CODE_STATUS 0xF0 |
| 61 | #define DS2482_PTR_CODE_DATA 0xE1 |
| 62 | #define DS2482_PTR_CODE_CHANNEL 0xD2 /* DS2482-800 only */ |
| 63 | #define DS2482_PTR_CODE_CONFIG 0xC3 |
| 64 | |
| 65 | /** |
| 66 | * Configure Register bit definitions |
| 67 | * The top 4 bits always read 0. |
| 68 | * To write, the top nibble must be the 1's compl. of the low nibble. |
| 69 | */ |
Michael Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 70 | #define DS2482_REG_CFG_1WS 0x08 /* 1-wire speed */ |
| 71 | #define DS2482_REG_CFG_SPU 0x04 /* strong pull-up */ |
| 72 | #define DS2482_REG_CFG_PPM 0x02 /* presence pulse masking */ |
| 73 | #define DS2482_REG_CFG_APU 0x01 /* active pull-up */ |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 74 | |
| 75 | |
| 76 | /** |
| 77 | * Write and verify codes for the CHANNEL_SELECT command (DS2482-800 only). |
| 78 | * To set the channel, write the value at the index of the channel. |
| 79 | * Read and compare against the corresponding value to verify the change. |
| 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 | * Status Register bit definitions (read only) |
| 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 | |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 99 | /* |
| 100 | * Client data (each client gets its own) |
| 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 { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 112 | struct i2c_client *client; |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 113 | struct mutex access_lock; |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 114 | |
| 115 | /* 1-wire interface(s) */ |
| 116 | int w1_count; /* 1 or 8 */ |
| 117 | struct ds2482_w1_chan w1_ch[8]; |
| 118 | |
| 119 | /* per-device values */ |
| 120 | u8 channel; |
| 121 | u8 read_prt; /* see DS2482_PTR_CODE_xxx */ |
| 122 | u8 reg_config; |
| 123 | }; |
| 124 | |
| 125 | |
| 126 | /** |
Michael Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 127 | * Helper to calculate values for configuration register |
| 128 | * @param conf the raw config value |
| 129 | * @return the value w/ complements that can be written to register |
| 130 | */ |
| 131 | static inline u8 ds2482_calculate_config(u8 conf) |
| 132 | { |
Mariusz Bialonczyk | 5cb27d3 | 2019-03-04 12:23:36 +0100 | [diff] [blame] | 133 | conf |= extra_config; |
| 134 | |
Mariusz Bialonczyk | f704938 | 2016-03-29 18:41:38 +0200 | [diff] [blame] | 135 | if (ds2482_active_pullup) |
| 136 | conf |= DS2482_REG_CFG_APU; |
| 137 | |
Michael Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 138 | return conf | ((~conf & 0x0f) << 4); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /** |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 143 | * Sets the read pointer. |
| 144 | * @param pdev The ds2482 client pointer |
| 145 | * @param read_ptr see DS2482_PTR_CODE_xxx above |
| 146 | * @return -1 on failure, 0 on success |
| 147 | */ |
| 148 | static inline int ds2482_select_register(struct ds2482_data *pdev, u8 read_ptr) |
| 149 | { |
| 150 | if (pdev->read_prt != read_ptr) { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 151 | if (i2c_smbus_write_byte_data(pdev->client, |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 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 | * Sends a command without a parameter |
| 163 | * @param pdev The ds2482 client pointer |
| 164 | * @param cmd DS2482_CMD_RESET, |
| 165 | * DS2482_CMD_1WIRE_RESET, |
| 166 | * DS2482_CMD_1WIRE_READ_BYTE |
| 167 | * @return -1 on failure, 0 on success |
| 168 | */ |
| 169 | static inline int ds2482_send_cmd(struct ds2482_data *pdev, u8 cmd) |
| 170 | { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 171 | if (i2c_smbus_write_byte(pdev->client, cmd) < 0) |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 172 | return -1; |
| 173 | |
| 174 | pdev->read_prt = DS2482_PTR_CODE_STATUS; |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Sends a command with a parameter |
| 180 | * @param pdev The ds2482 client pointer |
| 181 | * @param cmd DS2482_CMD_WRITE_CONFIG, |
| 182 | * DS2482_CMD_1WIRE_SINGLE_BIT, |
| 183 | * DS2482_CMD_1WIRE_WRITE_BYTE, |
| 184 | * DS2482_CMD_1WIRE_TRIPLET |
| 185 | * @param byte The data to send |
| 186 | * @return -1 on failure, 0 on success |
| 187 | */ |
| 188 | static inline int ds2482_send_cmd_data(struct ds2482_data *pdev, |
| 189 | u8 cmd, u8 byte) |
| 190 | { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 191 | if (i2c_smbus_write_byte_data(pdev->client, cmd, byte) < 0) |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 192 | return -1; |
| 193 | |
| 194 | /* all cmds leave in STATUS, except CONFIG */ |
| 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 | * 1-Wire interface code |
| 203 | */ |
| 204 | |
| 205 | #define DS2482_WAIT_IDLE_TIMEOUT 100 |
| 206 | |
| 207 | /** |
| 208 | * Waits until the 1-wire interface is idle (not busy) |
| 209 | * |
| 210 | * @param pdev Pointer to the device structure |
| 211 | * @return the last value read from status or -1 (failure) |
| 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 { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 220 | temp = i2c_smbus_read_byte(pdev->client); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 221 | } while ((temp >= 0) && (temp & DS2482_REG_STS_1WB) && |
Ben Gardner | b4786f1 | 2006-06-29 22:33:22 +0400 | [diff] [blame] | 222 | (++retries < DS2482_WAIT_IDLE_TIMEOUT)); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 223 | } |
| 224 | |
Roel Kluin | 6786073 | 2010-03-10 15:23:49 -0800 | [diff] [blame] | 225 | if (retries >= DS2482_WAIT_IDLE_TIMEOUT) |
Fjodor Schelichow | 1fda569 | 2014-06-19 02:52:01 +0200 | [diff] [blame] | 226 | pr_err("%s: timeout on channel %d\n", |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 227 | __func__, pdev->channel); |
| 228 | |
| 229 | return temp; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Selects a w1 channel. |
| 234 | * The 1-wire interface must be idle before calling this function. |
| 235 | * |
| 236 | * @param pdev The ds2482 client pointer |
| 237 | * @param channel 0-7 |
| 238 | * @return -1 (failure) or 0 (success) |
| 239 | */ |
| 240 | static int ds2482_set_channel(struct ds2482_data *pdev, u8 channel) |
| 241 | { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 242 | if (i2c_smbus_write_byte_data(pdev->client, DS2482_CMD_CHANNEL_SELECT, |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 243 | ds2482_chan_wr[channel]) < 0) |
| 244 | return -1; |
| 245 | |
| 246 | pdev->read_prt = DS2482_PTR_CODE_CHANNEL; |
| 247 | pdev->channel = -1; |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 248 | if (i2c_smbus_read_byte(pdev->client) == ds2482_chan_rd[channel]) { |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 249 | pdev->channel = channel; |
| 250 | return 0; |
| 251 | } |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | /** |
| 257 | * Performs the touch-bit function, which writes a 0 or 1 and reads the level. |
| 258 | * |
| 259 | * @param data The ds2482 channel pointer |
| 260 | * @param bit The level to write: 0 or non-zero |
| 261 | * @return The level read: 0 or 1 |
| 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 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 269 | mutex_lock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 270 | |
| 271 | /* Select the channel */ |
| 272 | ds2482_wait_1wire_idle(pdev); |
| 273 | if (pdev->w1_count > 1) |
| 274 | ds2482_set_channel(pdev, pchan->channel); |
| 275 | |
| 276 | /* Send the touch command, wait until 1WB == 0, return the status */ |
| 277 | if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_SINGLE_BIT, |
| 278 | bit ? 0xFF : 0)) |
| 279 | status = ds2482_wait_1wire_idle(pdev); |
| 280 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 281 | mutex_unlock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 282 | |
| 283 | return (status & DS2482_REG_STS_SBR) ? 1 : 0; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Performs the triplet function, which reads two bits and writes a bit. |
| 288 | * The bit written is determined by the two reads: |
| 289 | * 00 => dbit, 01 => 0, 10 => 1 |
| 290 | * |
| 291 | * @param data The ds2482 channel pointer |
| 292 | * @param dbit The direction to choose if both branches are valid |
| 293 | * @return b0=read1 b1=read2 b3=bit written |
| 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 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 301 | mutex_lock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 302 | |
| 303 | /* Select the channel */ |
| 304 | ds2482_wait_1wire_idle(pdev); |
| 305 | if (pdev->w1_count > 1) |
| 306 | ds2482_set_channel(pdev, pchan->channel); |
| 307 | |
| 308 | /* Send the triplet command, wait until 1WB == 0, return the status */ |
| 309 | if (!ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_TRIPLET, |
| 310 | dbit ? 0xFF : 0)) |
| 311 | status = ds2482_wait_1wire_idle(pdev); |
| 312 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 313 | mutex_unlock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 314 | |
| 315 | /* Decode the status */ |
| 316 | return (status >> 5); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Performs the write byte function. |
| 321 | * |
| 322 | * @param data The ds2482 channel pointer |
| 323 | * @param byte The value to write |
| 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 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 330 | mutex_lock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 331 | |
| 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 write byte command */ |
| 338 | ds2482_send_cmd_data(pdev, DS2482_CMD_1WIRE_WRITE_BYTE, byte); |
| 339 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 340 | mutex_unlock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Performs the read byte function. |
| 345 | * |
| 346 | * @param data The ds2482 channel pointer |
| 347 | * @return The value read |
| 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 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 355 | mutex_lock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 356 | |
| 357 | /* Select the channel */ |
| 358 | ds2482_wait_1wire_idle(pdev); |
| 359 | if (pdev->w1_count > 1) |
| 360 | ds2482_set_channel(pdev, pchan->channel); |
| 361 | |
| 362 | /* Send the read byte command */ |
| 363 | ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_READ_BYTE); |
| 364 | |
| 365 | /* Wait until 1WB == 0 */ |
| 366 | ds2482_wait_1wire_idle(pdev); |
| 367 | |
| 368 | /* Select the data register */ |
| 369 | ds2482_select_register(pdev, DS2482_PTR_CODE_DATA); |
| 370 | |
| 371 | /* Read the data byte */ |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 372 | result = i2c_smbus_read_byte(pdev->client); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 373 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 374 | mutex_unlock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 375 | |
| 376 | return result; |
| 377 | } |
| 378 | |
| 379 | |
| 380 | /** |
| 381 | * Sends a reset on the 1-wire interface |
| 382 | * |
| 383 | * @param data The ds2482 channel pointer |
| 384 | * @return 0=Device present, 1=No device present or error |
| 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 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 393 | mutex_lock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 394 | |
| 395 | /* Select the channel */ |
| 396 | ds2482_wait_1wire_idle(pdev); |
| 397 | if (pdev->w1_count > 1) |
| 398 | ds2482_set_channel(pdev, pchan->channel); |
| 399 | |
| 400 | /* Send the reset command */ |
| 401 | err = ds2482_send_cmd(pdev, DS2482_CMD_1WIRE_RESET); |
| 402 | if (err >= 0) { |
| 403 | /* Wait until the reset is complete */ |
| 404 | err = ds2482_wait_1wire_idle(pdev); |
| 405 | retval = !(err & DS2482_REG_STS_PPD); |
| 406 | |
| 407 | /* If the chip did reset since detect, re-config it */ |
| 408 | if (err & DS2482_REG_STS_RST) |
| 409 | ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG, |
Mariusz Bialonczyk | 5cb27d3 | 2019-03-04 12:23:36 +0100 | [diff] [blame] | 410 | ds2482_calculate_config(0x00)); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 411 | } |
| 412 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 413 | mutex_unlock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 414 | |
| 415 | return retval; |
| 416 | } |
| 417 | |
Michael Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 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 | /* if delay is non-zero activate the pullup, |
| 425 | * the strong pullup will be automatically deactivated |
| 426 | * by the master, so do not explicitly deactive it |
| 427 | */ |
| 428 | if (delay) { |
| 429 | /* both waits are crucial, otherwise devices might not be |
| 430 | * powered long enough, causing e.g. a w1_therm sensor to |
| 431 | * provide wrong conversion results |
| 432 | */ |
| 433 | ds2482_wait_1wire_idle(pdev); |
| 434 | /* note: it seems like both SPU and APU have to be set! */ |
| 435 | retval = ds2482_send_cmd_data(pdev, DS2482_CMD_WRITE_CONFIG, |
Mariusz Bialonczyk | 5cb27d3 | 2019-03-04 12:23:36 +0100 | [diff] [blame] | 436 | ds2482_calculate_config(DS2482_REG_CFG_SPU | |
| 437 | DS2482_REG_CFG_APU)); |
Michael Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 438 | ds2482_wait_1wire_idle(pdev); |
| 439 | } |
| 440 | |
| 441 | return retval; |
| 442 | } |
| 443 | |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 444 | |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 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; |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 452 | |
Jean Delvare | 0314b02 | 2009-10-04 22:53:41 +0200 | [diff] [blame] | 453 | if (!i2c_check_functionality(client->adapter, |
| 454 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA | |
| 455 | I2C_FUNC_SMBUS_BYTE)) |
| 456 | return -ENODEV; |
| 457 | |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 458 | if (!(data = kzalloc(sizeof(struct ds2482_data), GFP_KERNEL))) { |
| 459 | err = -ENOMEM; |
| 460 | goto exit; |
| 461 | } |
| 462 | |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 463 | data->client = client; |
| 464 | i2c_set_clientdata(client, data); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 465 | |
| 466 | /* Reset the device (sets the read_ptr to status) */ |
| 467 | if (ds2482_send_cmd(data, DS2482_CMD_RESET) < 0) { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 468 | dev_warn(&client->dev, "DS2482 reset failed.\n"); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 469 | goto exit_free; |
| 470 | } |
| 471 | |
| 472 | /* Sleep at least 525ns to allow the reset to complete */ |
| 473 | ndelay(525); |
| 474 | |
| 475 | /* Read the status byte - only reset bit and line should be set */ |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 476 | temp1 = i2c_smbus_read_byte(client); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 477 | if (temp1 != (DS2482_REG_STS_LL | DS2482_REG_STS_RST)) { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 478 | dev_warn(&client->dev, "DS2482 reset status " |
| 479 | "0x%02X - not a DS2482\n", temp1); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 480 | goto exit_free; |
| 481 | } |
| 482 | |
| 483 | /* Detect the 8-port version */ |
| 484 | data->w1_count = 1; |
| 485 | if (ds2482_set_channel(data, 7) == 0) |
| 486 | data->w1_count = 8; |
| 487 | |
| 488 | /* Set all config items to 0 (off) */ |
Michael Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 489 | ds2482_send_cmd_data(data, DS2482_CMD_WRITE_CONFIG, |
Mariusz Bialonczyk | 5cb27d3 | 2019-03-04 12:23:36 +0100 | [diff] [blame] | 490 | ds2482_calculate_config(0x00)); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 491 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 492 | mutex_init(&data->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 493 | |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 494 | /* Register 1-wire interface(s) */ |
| 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 | /* Populate all the w1 bus master stuff */ |
| 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; |
Michael Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 506 | data->w1_ch[idx].w1_bm.set_pullup = ds2482_w1_set_pullup; |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 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: |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 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 | |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 528 | static int ds2482_remove(struct i2c_client *client) |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 529 | { |
| 530 | struct ds2482_data *data = i2c_get_clientdata(client); |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 531 | int idx; |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 532 | |
| 533 | /* Unregister the 1-wire bridge(s) */ |
| 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 | |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 539 | /* Free the memory */ |
| 540 | kfree(data); |
| 541 | return 0; |
| 542 | } |
| 543 | |
Andrew F. Davis | 50fa295 | 2017-05-16 15:02:12 -0500 | [diff] [blame] | 544 | /** |
| 545 | * Driver data (common to all clients) |
| 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 | }; |
Wei Yongjun | dbfd5cc | 2012-10-08 22:06:07 +0800 | [diff] [blame] | 561 | module_i2c_driver(ds2482_driver); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 562 | |
| 563 | MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>"); |
| 564 | MODULE_DESCRIPTION("DS2482 driver"); |
Andrew Worsley | 5486531 | 2017-08-27 21:34:22 +1000 | [diff] [blame] | 565 | |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 566 | MODULE_LICENSE("GPL"); |