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> |
Todd Poynor | b1173ef | 2012-08-30 23:09:14 -0700 | [diff] [blame] | 21 | #include <linux/gpio.h> |
| 22 | #include <linux/platform_data/ds2482.h> |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 23 | #include <asm/delay.h> |
| 24 | |
| 25 | #include "../w1.h" |
| 26 | #include "../w1_int.h" |
| 27 | |
| 28 | /** |
Mariusz Bialonczyk | f704938 | 2016-03-29 18:41:38 +0200 | [diff] [blame] | 29 | * 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 | */ |
| 38 | static int ds2482_active_pullup = 1; |
| 39 | module_param_named(active_pullup, ds2482_active_pullup, int, 0644); |
| 40 | |
| 41 | /** |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 42 | * 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 Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 69 | #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 Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 73 | |
| 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 | */ |
| 80 | static const u8 ds2482_chan_wr[8] = |
| 81 | { 0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87 }; |
| 82 | static 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 Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 99 | static int ds2482_probe(struct i2c_client *client, |
| 100 | const struct i2c_device_id *id); |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 101 | static int ds2482_remove(struct i2c_client *client); |
Todd Poynor | b1173ef | 2012-08-30 23:09:14 -0700 | [diff] [blame] | 102 | static int ds2482_suspend(struct device *dev); |
| 103 | static int ds2482_resume(struct device *dev); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * Driver data (common to all clients) |
| 107 | */ |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 108 | static const struct i2c_device_id ds2482_id[] = { |
| 109 | { "ds2482", 0 }, |
| 110 | { } |
| 111 | }; |
Ludek Hlavacek | 0dcacd7 | 2015-05-27 00:37:59 +0200 | [diff] [blame] | 112 | MODULE_DEVICE_TABLE(i2c, ds2482_id); |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 113 | |
Todd Poynor | b1173ef | 2012-08-30 23:09:14 -0700 | [diff] [blame] | 114 | static const struct dev_pm_ops ds2482_pm_ops = { |
| 115 | .suspend = ds2482_suspend, |
| 116 | .resume = ds2482_resume, |
| 117 | }; |
| 118 | |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 119 | static struct i2c_driver ds2482_driver = { |
| 120 | .driver = { |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 121 | .name = "ds2482", |
Todd Poynor | b1173ef | 2012-08-30 23:09:14 -0700 | [diff] [blame] | 122 | .pm = &ds2482_pm_ops, |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 123 | }, |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 124 | .probe = ds2482_probe, |
| 125 | .remove = ds2482_remove, |
| 126 | .id_table = ds2482_id, |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | /* |
| 130 | * Client data (each client gets its own) |
| 131 | */ |
| 132 | |
| 133 | struct ds2482_data; |
| 134 | |
| 135 | struct ds2482_w1_chan { |
| 136 | struct ds2482_data *pdev; |
| 137 | u8 channel; |
| 138 | struct w1_bus_master w1_bm; |
| 139 | }; |
| 140 | |
| 141 | struct ds2482_data { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 142 | struct i2c_client *client; |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 143 | struct mutex access_lock; |
Todd Poynor | b1173ef | 2012-08-30 23:09:14 -0700 | [diff] [blame] | 144 | int slpz_gpio; |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 145 | |
| 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 Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 158 | * 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 | */ |
| 162 | static inline u8 ds2482_calculate_config(u8 conf) |
| 163 | { |
Mariusz Bialonczyk | f704938 | 2016-03-29 18:41:38 +0200 | [diff] [blame] | 164 | if (ds2482_active_pullup) |
| 165 | conf |= DS2482_REG_CFG_APU; |
| 166 | |
Michael Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 167 | return conf | ((~conf & 0x0f) << 4); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /** |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 172 | * 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 | */ |
| 177 | static inline int ds2482_select_register(struct ds2482_data *pdev, u8 read_ptr) |
| 178 | { |
| 179 | if (pdev->read_prt != read_ptr) { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 180 | if (i2c_smbus_write_byte_data(pdev->client, |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 181 | 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 | */ |
| 198 | static inline int ds2482_send_cmd(struct ds2482_data *pdev, u8 cmd) |
| 199 | { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 200 | if (i2c_smbus_write_byte(pdev->client, cmd) < 0) |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 201 | 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 | */ |
| 217 | static inline int ds2482_send_cmd_data(struct ds2482_data *pdev, |
| 218 | u8 cmd, u8 byte) |
| 219 | { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 220 | if (i2c_smbus_write_byte_data(pdev->client, cmd, byte) < 0) |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 221 | 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 | */ |
| 242 | static 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 Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 249 | temp = i2c_smbus_read_byte(pdev->client); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 250 | } while ((temp >= 0) && (temp & DS2482_REG_STS_1WB) && |
Ben Gardner | b4786f1 | 2006-06-29 22:33:22 +0400 | [diff] [blame] | 251 | (++retries < DS2482_WAIT_IDLE_TIMEOUT)); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 252 | } |
| 253 | |
Roel Kluin | 6786073 | 2010-03-10 15:23:49 -0800 | [diff] [blame] | 254 | if (retries >= DS2482_WAIT_IDLE_TIMEOUT) |
Fjodor Schelichow | 1fda569 | 2014-06-19 02:52:01 +0200 | [diff] [blame] | 255 | pr_err("%s: timeout on channel %d\n", |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 256 | __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 | */ |
| 269 | static int ds2482_set_channel(struct ds2482_data *pdev, u8 channel) |
| 270 | { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 271 | if (i2c_smbus_write_byte_data(pdev->client, DS2482_CMD_CHANNEL_SELECT, |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 272 | ds2482_chan_wr[channel]) < 0) |
| 273 | return -1; |
| 274 | |
| 275 | pdev->read_prt = DS2482_PTR_CODE_CHANNEL; |
| 276 | pdev->channel = -1; |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 277 | if (i2c_smbus_read_byte(pdev->client) == ds2482_chan_rd[channel]) { |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 278 | 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 | */ |
| 292 | static 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 Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 298 | mutex_lock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 299 | |
| 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 Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 310 | mutex_unlock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 311 | |
| 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 | */ |
| 324 | static 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 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 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 Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 342 | mutex_unlock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 343 | |
| 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 | */ |
| 354 | static 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 Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 359 | mutex_lock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 360 | |
| 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 Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 369 | mutex_unlock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Performs the read byte function. |
| 374 | * |
| 375 | * @param data The ds2482 channel pointer |
| 376 | * @return The value read |
| 377 | */ |
| 378 | static 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 Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 384 | mutex_lock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 385 | |
| 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 Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 401 | result = i2c_smbus_read_byte(pdev->client); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 402 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 403 | mutex_unlock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 404 | |
| 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 | */ |
| 415 | static 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 Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 422 | mutex_lock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 423 | |
| 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 Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 439 | ds2482_calculate_config(0x00)); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 440 | } |
| 441 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 442 | mutex_unlock(&pdev->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 443 | |
| 444 | return retval; |
| 445 | } |
| 446 | |
Michael Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 447 | static 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 Poynor | b1173ef | 2012-08-30 23:09:14 -0700 | [diff] [blame] | 473 | static 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 | |
| 483 | static 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 Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 492 | |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 493 | static int ds2482_probe(struct i2c_client *client, |
| 494 | const struct i2c_device_id *id) |
| 495 | { |
| 496 | struct ds2482_data *data; |
Todd Poynor | b1173ef | 2012-08-30 23:09:14 -0700 | [diff] [blame] | 497 | struct ds2482_platform_data *pdata; |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 498 | int err = -ENODEV; |
| 499 | int temp1; |
| 500 | int idx; |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 501 | |
Jean Delvare | 0314b02 | 2009-10-04 22:53:41 +0200 | [diff] [blame] | 502 | if (!i2c_check_functionality(client->adapter, |
| 503 | I2C_FUNC_SMBUS_WRITE_BYTE_DATA | |
| 504 | I2C_FUNC_SMBUS_BYTE)) |
| 505 | return -ENODEV; |
| 506 | |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 507 | if (!(data = kzalloc(sizeof(struct ds2482_data), GFP_KERNEL))) { |
| 508 | err = -ENOMEM; |
| 509 | goto exit; |
| 510 | } |
| 511 | |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 512 | data->client = client; |
| 513 | i2c_set_clientdata(client, data); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 514 | |
| 515 | /* Reset the device (sets the read_ptr to status) */ |
| 516 | if (ds2482_send_cmd(data, DS2482_CMD_RESET) < 0) { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 517 | dev_warn(&client->dev, "DS2482 reset failed.\n"); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 518 | 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 Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 525 | temp1 = i2c_smbus_read_byte(client); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 526 | if (temp1 != (DS2482_REG_STS_LL | DS2482_REG_STS_RST)) { |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 527 | dev_warn(&client->dev, "DS2482 reset status " |
| 528 | "0x%02X - not a DS2482\n", temp1); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 529 | 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 Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 538 | ds2482_send_cmd_data(data, DS2482_CMD_WRITE_CONFIG, |
| 539 | ds2482_calculate_config(0x00)); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 540 | |
Evgeniy Polyakov | abd52a1 | 2006-04-03 12:04:27 +0400 | [diff] [blame] | 541 | mutex_init(&data->access_lock); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 542 | |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 543 | /* 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 Arndt | 9c95bb6 | 2013-02-17 20:31:27 +0100 | [diff] [blame] | 555 | data->w1_ch[idx].w1_bm.set_pullup = ds2482_w1_set_pullup; |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 556 | |
| 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 Poynor | b1173ef | 2012-08-30 23:09:14 -0700 | [diff] [blame] | 564 | 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 Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 574 | return 0; |
| 575 | |
| 576 | exit_w1_remove: |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 577 | 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 | } |
| 581 | exit_free: |
| 582 | kfree(data); |
| 583 | exit: |
| 584 | return err; |
| 585 | } |
| 586 | |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 587 | static int ds2482_remove(struct i2c_client *client) |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 588 | { |
| 589 | struct ds2482_data *data = i2c_get_clientdata(client); |
Jean Delvare | 61c91f7 | 2008-07-16 19:30:07 +0200 | [diff] [blame] | 590 | int idx; |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 591 | |
| 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 Poynor | b1173ef | 2012-08-30 23:09:14 -0700 | [diff] [blame] | 598 | if (data->slpz_gpio >= 0) { |
| 599 | gpio_set_value(data->slpz_gpio, 0); |
| 600 | gpio_free(data->slpz_gpio); |
| 601 | } |
| 602 | |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 603 | /* Free the memory */ |
| 604 | kfree(data); |
| 605 | return 0; |
| 606 | } |
| 607 | |
Wei Yongjun | dbfd5cc | 2012-10-08 22:06:07 +0800 | [diff] [blame] | 608 | module_i2c_driver(ds2482_driver); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 609 | |
Mariusz Bialonczyk | f704938 | 2016-03-29 18:41:38 +0200 | [diff] [blame] | 610 | MODULE_PARM_DESC(active_pullup, "Active pullup (apply to all buses): " \ |
| 611 | "0-disable, 1-enable (default)"); |
Evgeniy Polyakov | baf12ae | 2005-12-06 13:38:28 +0300 | [diff] [blame] | 612 | MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>"); |
| 613 | MODULE_DESCRIPTION("DS2482 driver"); |
| 614 | MODULE_LICENSE("GPL"); |