Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * drivers/w1/masters/omap_hdq.c |
| 3 | * |
Paul Walmsley | c354a86 | 2012-06-21 21:40:40 -0600 | [diff] [blame^] | 4 | * Copyright (C) 2007,2012 Texas Instruments, Inc. |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 5 | * |
| 6 | * This file is licensed under the terms of the GNU General Public License |
| 7 | * version 2. This program is licensed "as is" without any warranty of any |
| 8 | * kind, whether express or implied. |
| 9 | * |
| 10 | */ |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/interrupt.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 16 | #include <linux/err.h> |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 17 | #include <linux/io.h> |
Amit Kucheria | 81fa08f | 2010-04-23 13:18:03 -0400 | [diff] [blame] | 18 | #include <linux/sched.h> |
Paul Walmsley | c354a86 | 2012-06-21 21:40:40 -0600 | [diff] [blame^] | 19 | #include <linux/pm_runtime.h> |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 20 | |
| 21 | #include <asm/irq.h> |
| 22 | #include <mach/hardware.h> |
| 23 | |
| 24 | #include "../w1.h" |
| 25 | #include "../w1_int.h" |
| 26 | |
| 27 | #define MOD_NAME "OMAP_HDQ:" |
| 28 | |
| 29 | #define OMAP_HDQ_REVISION 0x00 |
| 30 | #define OMAP_HDQ_TX_DATA 0x04 |
| 31 | #define OMAP_HDQ_RX_DATA 0x08 |
| 32 | #define OMAP_HDQ_CTRL_STATUS 0x0c |
| 33 | #define OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK (1<<6) |
| 34 | #define OMAP_HDQ_CTRL_STATUS_CLOCKENABLE (1<<5) |
| 35 | #define OMAP_HDQ_CTRL_STATUS_GO (1<<4) |
| 36 | #define OMAP_HDQ_CTRL_STATUS_INITIALIZATION (1<<2) |
| 37 | #define OMAP_HDQ_CTRL_STATUS_DIR (1<<1) |
| 38 | #define OMAP_HDQ_CTRL_STATUS_MODE (1<<0) |
| 39 | #define OMAP_HDQ_INT_STATUS 0x10 |
| 40 | #define OMAP_HDQ_INT_STATUS_TXCOMPLETE (1<<2) |
| 41 | #define OMAP_HDQ_INT_STATUS_RXCOMPLETE (1<<1) |
| 42 | #define OMAP_HDQ_INT_STATUS_TIMEOUT (1<<0) |
| 43 | #define OMAP_HDQ_SYSCONFIG 0x14 |
| 44 | #define OMAP_HDQ_SYSCONFIG_SOFTRESET (1<<1) |
| 45 | #define OMAP_HDQ_SYSCONFIG_AUTOIDLE (1<<0) |
| 46 | #define OMAP_HDQ_SYSSTATUS 0x18 |
| 47 | #define OMAP_HDQ_SYSSTATUS_RESETDONE (1<<0) |
| 48 | |
| 49 | #define OMAP_HDQ_FLAG_CLEAR 0 |
| 50 | #define OMAP_HDQ_FLAG_SET 1 |
| 51 | #define OMAP_HDQ_TIMEOUT (HZ/5) |
| 52 | |
| 53 | #define OMAP_HDQ_MAX_USER 4 |
| 54 | |
| 55 | static DECLARE_WAIT_QUEUE_HEAD(hdq_wait_queue); |
| 56 | static int w1_id; |
| 57 | |
| 58 | struct hdq_data { |
| 59 | struct device *dev; |
| 60 | void __iomem *hdq_base; |
| 61 | /* lock status update */ |
| 62 | struct mutex hdq_mutex; |
| 63 | int hdq_usecount; |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 64 | u8 hdq_irqstatus; |
| 65 | /* device lock */ |
| 66 | spinlock_t hdq_spinlock; |
| 67 | /* |
| 68 | * Used to control the call to omap_hdq_get and omap_hdq_put. |
| 69 | * HDQ Protocol: Write the CMD|REG_address first, followed by |
| 70 | * the data wrire or read. |
| 71 | */ |
| 72 | int init_trans; |
| 73 | }; |
| 74 | |
Uwe Kleine-König | a96b912 | 2010-02-04 20:56:54 +0100 | [diff] [blame] | 75 | static int __devinit omap_hdq_probe(struct platform_device *pdev); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 76 | static int omap_hdq_remove(struct platform_device *pdev); |
| 77 | |
| 78 | static struct platform_driver omap_hdq_driver = { |
| 79 | .probe = omap_hdq_probe, |
| 80 | .remove = omap_hdq_remove, |
| 81 | .driver = { |
| 82 | .name = "omap_hdq", |
| 83 | }, |
| 84 | }; |
| 85 | |
| 86 | static u8 omap_w1_read_byte(void *_hdq); |
| 87 | static void omap_w1_write_byte(void *_hdq, u8 byte); |
| 88 | static u8 omap_w1_reset_bus(void *_hdq); |
Stanley.Miao | 06b0d4d | 2008-11-19 15:36:50 -0800 | [diff] [blame] | 89 | static void omap_w1_search_bus(void *_hdq, struct w1_master *master_dev, |
| 90 | u8 search_type, w1_slave_found_callback slave_found); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 91 | |
| 92 | |
| 93 | static struct w1_bus_master omap_w1_master = { |
| 94 | .read_byte = omap_w1_read_byte, |
| 95 | .write_byte = omap_w1_write_byte, |
| 96 | .reset_bus = omap_w1_reset_bus, |
| 97 | .search = omap_w1_search_bus, |
| 98 | }; |
| 99 | |
| 100 | /* HDQ register I/O routines */ |
| 101 | static inline u8 hdq_reg_in(struct hdq_data *hdq_data, u32 offset) |
| 102 | { |
Paul Walmsley | 2acd0894 | 2012-06-21 21:40:37 -0600 | [diff] [blame] | 103 | return __raw_readl(hdq_data->hdq_base + offset); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static inline void hdq_reg_out(struct hdq_data *hdq_data, u32 offset, u8 val) |
| 107 | { |
Paul Walmsley | 2acd0894 | 2012-06-21 21:40:37 -0600 | [diff] [blame] | 108 | __raw_writel(val, hdq_data->hdq_base + offset); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static inline u8 hdq_reg_merge(struct hdq_data *hdq_data, u32 offset, |
| 112 | u8 val, u8 mask) |
| 113 | { |
Paul Walmsley | 2acd0894 | 2012-06-21 21:40:37 -0600 | [diff] [blame] | 114 | u8 new_val = (__raw_readl(hdq_data->hdq_base + offset) & ~mask) |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 115 | | (val & mask); |
Paul Walmsley | 2acd0894 | 2012-06-21 21:40:37 -0600 | [diff] [blame] | 116 | __raw_writel(new_val, hdq_data->hdq_base + offset); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 117 | |
| 118 | return new_val; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Wait for one or more bits in flag change. |
| 123 | * HDQ_FLAG_SET: wait until any bit in the flag is set. |
| 124 | * HDQ_FLAG_CLEAR: wait until all bits in the flag are cleared. |
| 125 | * return 0 on success and -ETIMEDOUT in the case of timeout. |
| 126 | */ |
| 127 | static int hdq_wait_for_flag(struct hdq_data *hdq_data, u32 offset, |
| 128 | u8 flag, u8 flag_set, u8 *status) |
| 129 | { |
| 130 | int ret = 0; |
| 131 | unsigned long timeout = jiffies + OMAP_HDQ_TIMEOUT; |
| 132 | |
| 133 | if (flag_set == OMAP_HDQ_FLAG_CLEAR) { |
| 134 | /* wait for the flag clear */ |
| 135 | while (((*status = hdq_reg_in(hdq_data, offset)) & flag) |
| 136 | && time_before(jiffies, timeout)) { |
| 137 | schedule_timeout_uninterruptible(1); |
| 138 | } |
| 139 | if (*status & flag) |
| 140 | ret = -ETIMEDOUT; |
| 141 | } else if (flag_set == OMAP_HDQ_FLAG_SET) { |
| 142 | /* wait for the flag set */ |
| 143 | while (!((*status = hdq_reg_in(hdq_data, offset)) & flag) |
| 144 | && time_before(jiffies, timeout)) { |
| 145 | schedule_timeout_uninterruptible(1); |
| 146 | } |
| 147 | if (!(*status & flag)) |
| 148 | ret = -ETIMEDOUT; |
| 149 | } else |
| 150 | return -EINVAL; |
| 151 | |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | /* write out a byte and fill *status with HDQ_INT_STATUS */ |
| 156 | static int hdq_write_byte(struct hdq_data *hdq_data, u8 val, u8 *status) |
| 157 | { |
| 158 | int ret; |
| 159 | u8 tmp_status; |
| 160 | unsigned long irqflags; |
| 161 | |
| 162 | *status = 0; |
| 163 | |
| 164 | spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags); |
| 165 | /* clear interrupt flags via a dummy read */ |
| 166 | hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS); |
| 167 | /* ISR loads it with new INT_STATUS */ |
| 168 | hdq_data->hdq_irqstatus = 0; |
| 169 | spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags); |
| 170 | |
| 171 | hdq_reg_out(hdq_data, OMAP_HDQ_TX_DATA, val); |
| 172 | |
| 173 | /* set the GO bit */ |
| 174 | hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, OMAP_HDQ_CTRL_STATUS_GO, |
| 175 | OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO); |
| 176 | /* wait for the TXCOMPLETE bit */ |
| 177 | ret = wait_event_timeout(hdq_wait_queue, |
| 178 | hdq_data->hdq_irqstatus, OMAP_HDQ_TIMEOUT); |
| 179 | if (ret == 0) { |
| 180 | dev_dbg(hdq_data->dev, "TX wait elapsed\n"); |
| 181 | goto out; |
| 182 | } |
| 183 | |
| 184 | *status = hdq_data->hdq_irqstatus; |
| 185 | /* check irqstatus */ |
| 186 | if (!(*status & OMAP_HDQ_INT_STATUS_TXCOMPLETE)) { |
| 187 | dev_dbg(hdq_data->dev, "timeout waiting for" |
| 188 | "TXCOMPLETE/RXCOMPLETE, %x", *status); |
| 189 | ret = -ETIMEDOUT; |
| 190 | goto out; |
| 191 | } |
| 192 | |
| 193 | /* wait for the GO bit return to zero */ |
| 194 | ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_CTRL_STATUS, |
| 195 | OMAP_HDQ_CTRL_STATUS_GO, |
| 196 | OMAP_HDQ_FLAG_CLEAR, &tmp_status); |
| 197 | if (ret) { |
| 198 | dev_dbg(hdq_data->dev, "timeout waiting GO bit" |
| 199 | "return to zero, %x", tmp_status); |
| 200 | } |
| 201 | |
| 202 | out: |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | /* HDQ Interrupt service routine */ |
| 207 | static irqreturn_t hdq_isr(int irq, void *_hdq) |
| 208 | { |
| 209 | struct hdq_data *hdq_data = _hdq; |
| 210 | unsigned long irqflags; |
| 211 | |
| 212 | spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags); |
| 213 | hdq_data->hdq_irqstatus = hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS); |
| 214 | spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags); |
| 215 | dev_dbg(hdq_data->dev, "hdq_isr: %x", hdq_data->hdq_irqstatus); |
| 216 | |
| 217 | if (hdq_data->hdq_irqstatus & |
| 218 | (OMAP_HDQ_INT_STATUS_TXCOMPLETE | OMAP_HDQ_INT_STATUS_RXCOMPLETE |
| 219 | | OMAP_HDQ_INT_STATUS_TIMEOUT)) { |
| 220 | /* wake up sleeping process */ |
| 221 | wake_up(&hdq_wait_queue); |
| 222 | } |
| 223 | |
| 224 | return IRQ_HANDLED; |
| 225 | } |
| 226 | |
| 227 | /* HDQ Mode: always return success */ |
| 228 | static u8 omap_w1_reset_bus(void *_hdq) |
| 229 | { |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | /* W1 search callback function */ |
Stanley.Miao | 06b0d4d | 2008-11-19 15:36:50 -0800 | [diff] [blame] | 234 | static void omap_w1_search_bus(void *_hdq, struct w1_master *master_dev, |
| 235 | u8 search_type, w1_slave_found_callback slave_found) |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 236 | { |
| 237 | u64 module_id, rn_le, cs, id; |
| 238 | |
| 239 | if (w1_id) |
| 240 | module_id = w1_id; |
| 241 | else |
| 242 | module_id = 0x1; |
| 243 | |
| 244 | rn_le = cpu_to_le64(module_id); |
| 245 | /* |
| 246 | * HDQ might not obey truly the 1-wire spec. |
| 247 | * So calculate CRC based on module parameter. |
| 248 | */ |
| 249 | cs = w1_calc_crc8((u8 *)&rn_le, 7); |
| 250 | id = (cs << 56) | module_id; |
| 251 | |
Stanley.Miao | 06b0d4d | 2008-11-19 15:36:50 -0800 | [diff] [blame] | 252 | slave_found(master_dev, id); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static int _omap_hdq_reset(struct hdq_data *hdq_data) |
| 256 | { |
| 257 | int ret; |
| 258 | u8 tmp_status; |
| 259 | |
| 260 | hdq_reg_out(hdq_data, OMAP_HDQ_SYSCONFIG, OMAP_HDQ_SYSCONFIG_SOFTRESET); |
| 261 | /* |
| 262 | * Select HDQ mode & enable clocks. |
| 263 | * It is observed that INT flags can't be cleared via a read and GO/INIT |
| 264 | * won't return to zero if interrupt is disabled. So we always enable |
| 265 | * interrupt. |
| 266 | */ |
| 267 | hdq_reg_out(hdq_data, OMAP_HDQ_CTRL_STATUS, |
| 268 | OMAP_HDQ_CTRL_STATUS_CLOCKENABLE | |
| 269 | OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK); |
| 270 | |
| 271 | /* wait for reset to complete */ |
| 272 | ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_SYSSTATUS, |
| 273 | OMAP_HDQ_SYSSTATUS_RESETDONE, OMAP_HDQ_FLAG_SET, &tmp_status); |
| 274 | if (ret) |
| 275 | dev_dbg(hdq_data->dev, "timeout waiting HDQ reset, %x", |
| 276 | tmp_status); |
| 277 | else { |
| 278 | hdq_reg_out(hdq_data, OMAP_HDQ_CTRL_STATUS, |
| 279 | OMAP_HDQ_CTRL_STATUS_CLOCKENABLE | |
| 280 | OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK); |
| 281 | hdq_reg_out(hdq_data, OMAP_HDQ_SYSCONFIG, |
| 282 | OMAP_HDQ_SYSCONFIG_AUTOIDLE); |
| 283 | } |
| 284 | |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | /* Issue break pulse to the device */ |
| 289 | static int omap_hdq_break(struct hdq_data *hdq_data) |
| 290 | { |
| 291 | int ret = 0; |
| 292 | u8 tmp_status; |
| 293 | unsigned long irqflags; |
| 294 | |
| 295 | ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); |
| 296 | if (ret < 0) { |
| 297 | dev_dbg(hdq_data->dev, "Could not acquire mutex\n"); |
| 298 | ret = -EINTR; |
| 299 | goto rtn; |
| 300 | } |
| 301 | |
| 302 | spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags); |
| 303 | /* clear interrupt flags via a dummy read */ |
| 304 | hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS); |
| 305 | /* ISR loads it with new INT_STATUS */ |
| 306 | hdq_data->hdq_irqstatus = 0; |
| 307 | spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags); |
| 308 | |
| 309 | /* set the INIT and GO bit */ |
| 310 | hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, |
| 311 | OMAP_HDQ_CTRL_STATUS_INITIALIZATION | OMAP_HDQ_CTRL_STATUS_GO, |
| 312 | OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_INITIALIZATION | |
| 313 | OMAP_HDQ_CTRL_STATUS_GO); |
| 314 | |
| 315 | /* wait for the TIMEOUT bit */ |
| 316 | ret = wait_event_timeout(hdq_wait_queue, |
| 317 | hdq_data->hdq_irqstatus, OMAP_HDQ_TIMEOUT); |
| 318 | if (ret == 0) { |
| 319 | dev_dbg(hdq_data->dev, "break wait elapsed\n"); |
| 320 | ret = -EINTR; |
| 321 | goto out; |
| 322 | } |
| 323 | |
| 324 | tmp_status = hdq_data->hdq_irqstatus; |
| 325 | /* check irqstatus */ |
| 326 | if (!(tmp_status & OMAP_HDQ_INT_STATUS_TIMEOUT)) { |
| 327 | dev_dbg(hdq_data->dev, "timeout waiting for TIMEOUT, %x", |
| 328 | tmp_status); |
| 329 | ret = -ETIMEDOUT; |
| 330 | goto out; |
| 331 | } |
| 332 | /* |
| 333 | * wait for both INIT and GO bits rerurn to zero. |
| 334 | * zero wait time expected for interrupt mode. |
| 335 | */ |
| 336 | ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_CTRL_STATUS, |
| 337 | OMAP_HDQ_CTRL_STATUS_INITIALIZATION | |
| 338 | OMAP_HDQ_CTRL_STATUS_GO, OMAP_HDQ_FLAG_CLEAR, |
| 339 | &tmp_status); |
| 340 | if (ret) |
| 341 | dev_dbg(hdq_data->dev, "timeout waiting INIT&GO bits" |
| 342 | "return to zero, %x", tmp_status); |
| 343 | |
| 344 | out: |
| 345 | mutex_unlock(&hdq_data->hdq_mutex); |
| 346 | rtn: |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | static int hdq_read_byte(struct hdq_data *hdq_data, u8 *val) |
| 351 | { |
| 352 | int ret = 0; |
| 353 | u8 status; |
| 354 | unsigned long timeout = jiffies + OMAP_HDQ_TIMEOUT; |
| 355 | |
| 356 | ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); |
| 357 | if (ret < 0) { |
| 358 | ret = -EINTR; |
| 359 | goto rtn; |
| 360 | } |
| 361 | |
| 362 | if (!hdq_data->hdq_usecount) { |
| 363 | ret = -EINVAL; |
| 364 | goto out; |
| 365 | } |
| 366 | |
| 367 | if (!(hdq_data->hdq_irqstatus & OMAP_HDQ_INT_STATUS_RXCOMPLETE)) { |
| 368 | hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, |
| 369 | OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO, |
| 370 | OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO); |
| 371 | /* |
| 372 | * The RX comes immediately after TX. It |
| 373 | * triggers another interrupt before we |
| 374 | * sleep. So we have to wait for RXCOMPLETE bit. |
| 375 | */ |
| 376 | while (!(hdq_data->hdq_irqstatus |
| 377 | & OMAP_HDQ_INT_STATUS_RXCOMPLETE) |
| 378 | && time_before(jiffies, timeout)) { |
| 379 | schedule_timeout_uninterruptible(1); |
| 380 | } |
| 381 | hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, 0, |
| 382 | OMAP_HDQ_CTRL_STATUS_DIR); |
| 383 | status = hdq_data->hdq_irqstatus; |
| 384 | /* check irqstatus */ |
| 385 | if (!(status & OMAP_HDQ_INT_STATUS_RXCOMPLETE)) { |
| 386 | dev_dbg(hdq_data->dev, "timeout waiting for" |
| 387 | "RXCOMPLETE, %x", status); |
| 388 | ret = -ETIMEDOUT; |
| 389 | goto out; |
| 390 | } |
| 391 | } |
| 392 | /* the data is ready. Read it in! */ |
| 393 | *val = hdq_reg_in(hdq_data, OMAP_HDQ_RX_DATA); |
| 394 | out: |
| 395 | mutex_unlock(&hdq_data->hdq_mutex); |
| 396 | rtn: |
| 397 | return 0; |
| 398 | |
| 399 | } |
| 400 | |
| 401 | /* Enable clocks and set the controller to HDQ mode */ |
| 402 | static int omap_hdq_get(struct hdq_data *hdq_data) |
| 403 | { |
| 404 | int ret = 0; |
| 405 | |
| 406 | ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); |
| 407 | if (ret < 0) { |
| 408 | ret = -EINTR; |
| 409 | goto rtn; |
| 410 | } |
| 411 | |
| 412 | if (OMAP_HDQ_MAX_USER == hdq_data->hdq_usecount) { |
| 413 | dev_dbg(hdq_data->dev, "attempt to exceed the max use count"); |
| 414 | ret = -EINVAL; |
| 415 | goto out; |
| 416 | } else { |
| 417 | hdq_data->hdq_usecount++; |
| 418 | try_module_get(THIS_MODULE); |
| 419 | if (1 == hdq_data->hdq_usecount) { |
Paul Walmsley | c354a86 | 2012-06-21 21:40:40 -0600 | [diff] [blame^] | 420 | |
| 421 | pm_runtime_get_sync(hdq_data->dev); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 422 | |
| 423 | /* make sure HDQ is out of reset */ |
| 424 | if (!(hdq_reg_in(hdq_data, OMAP_HDQ_SYSSTATUS) & |
| 425 | OMAP_HDQ_SYSSTATUS_RESETDONE)) { |
| 426 | ret = _omap_hdq_reset(hdq_data); |
| 427 | if (ret) |
| 428 | /* back up the count */ |
| 429 | hdq_data->hdq_usecount--; |
| 430 | } else { |
| 431 | /* select HDQ mode & enable clocks */ |
| 432 | hdq_reg_out(hdq_data, OMAP_HDQ_CTRL_STATUS, |
| 433 | OMAP_HDQ_CTRL_STATUS_CLOCKENABLE | |
| 434 | OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK); |
| 435 | hdq_reg_out(hdq_data, OMAP_HDQ_SYSCONFIG, |
| 436 | OMAP_HDQ_SYSCONFIG_AUTOIDLE); |
| 437 | hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 442 | out: |
| 443 | mutex_unlock(&hdq_data->hdq_mutex); |
| 444 | rtn: |
| 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | /* Disable clocks to the module */ |
| 449 | static int omap_hdq_put(struct hdq_data *hdq_data) |
| 450 | { |
| 451 | int ret = 0; |
| 452 | |
| 453 | ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); |
| 454 | if (ret < 0) |
| 455 | return -EINTR; |
| 456 | |
| 457 | if (0 == hdq_data->hdq_usecount) { |
| 458 | dev_dbg(hdq_data->dev, "attempt to decrement use count" |
| 459 | "when it is zero"); |
| 460 | ret = -EINVAL; |
| 461 | } else { |
| 462 | hdq_data->hdq_usecount--; |
| 463 | module_put(THIS_MODULE); |
Paul Walmsley | c354a86 | 2012-06-21 21:40:40 -0600 | [diff] [blame^] | 464 | if (0 == hdq_data->hdq_usecount) |
| 465 | pm_runtime_put_sync(hdq_data->dev); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 466 | } |
| 467 | mutex_unlock(&hdq_data->hdq_mutex); |
| 468 | |
| 469 | return ret; |
| 470 | } |
| 471 | |
| 472 | /* Read a byte of data from the device */ |
| 473 | static u8 omap_w1_read_byte(void *_hdq) |
| 474 | { |
| 475 | struct hdq_data *hdq_data = _hdq; |
| 476 | u8 val = 0; |
| 477 | int ret; |
| 478 | |
| 479 | ret = hdq_read_byte(hdq_data, &val); |
| 480 | if (ret) { |
| 481 | ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); |
| 482 | if (ret < 0) { |
| 483 | dev_dbg(hdq_data->dev, "Could not acquire mutex\n"); |
| 484 | return -EINTR; |
| 485 | } |
| 486 | hdq_data->init_trans = 0; |
| 487 | mutex_unlock(&hdq_data->hdq_mutex); |
| 488 | omap_hdq_put(hdq_data); |
| 489 | return -1; |
| 490 | } |
| 491 | |
| 492 | /* Write followed by a read, release the module */ |
| 493 | if (hdq_data->init_trans) { |
| 494 | ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); |
| 495 | if (ret < 0) { |
| 496 | dev_dbg(hdq_data->dev, "Could not acquire mutex\n"); |
| 497 | return -EINTR; |
| 498 | } |
| 499 | hdq_data->init_trans = 0; |
| 500 | mutex_unlock(&hdq_data->hdq_mutex); |
| 501 | omap_hdq_put(hdq_data); |
| 502 | } |
| 503 | |
| 504 | return val; |
| 505 | } |
| 506 | |
| 507 | /* Write a byte of data to the device */ |
| 508 | static void omap_w1_write_byte(void *_hdq, u8 byte) |
| 509 | { |
| 510 | struct hdq_data *hdq_data = _hdq; |
| 511 | int ret; |
| 512 | u8 status; |
| 513 | |
| 514 | /* First write to initialize the transfer */ |
| 515 | if (hdq_data->init_trans == 0) |
| 516 | omap_hdq_get(hdq_data); |
| 517 | |
| 518 | ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); |
| 519 | if (ret < 0) { |
| 520 | dev_dbg(hdq_data->dev, "Could not acquire mutex\n"); |
| 521 | return; |
| 522 | } |
| 523 | hdq_data->init_trans++; |
| 524 | mutex_unlock(&hdq_data->hdq_mutex); |
| 525 | |
| 526 | ret = hdq_write_byte(hdq_data, byte, &status); |
| 527 | if (ret == 0) { |
| 528 | dev_dbg(hdq_data->dev, "TX failure:Ctrl status %x\n", status); |
| 529 | return; |
| 530 | } |
| 531 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 532 | /* Second write, data transferred. Release the module */ |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 533 | if (hdq_data->init_trans > 1) { |
| 534 | omap_hdq_put(hdq_data); |
| 535 | ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); |
| 536 | if (ret < 0) { |
| 537 | dev_dbg(hdq_data->dev, "Could not acquire mutex\n"); |
| 538 | return; |
| 539 | } |
| 540 | hdq_data->init_trans = 0; |
| 541 | mutex_unlock(&hdq_data->hdq_mutex); |
| 542 | } |
| 543 | |
| 544 | return; |
| 545 | } |
| 546 | |
Uwe Kleine-König | a96b912 | 2010-02-04 20:56:54 +0100 | [diff] [blame] | 547 | static int __devinit omap_hdq_probe(struct platform_device *pdev) |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 548 | { |
| 549 | struct hdq_data *hdq_data; |
| 550 | struct resource *res; |
| 551 | int ret, irq; |
| 552 | u8 rev; |
| 553 | |
| 554 | hdq_data = kmalloc(sizeof(*hdq_data), GFP_KERNEL); |
| 555 | if (!hdq_data) { |
| 556 | dev_dbg(&pdev->dev, "unable to allocate memory\n"); |
| 557 | ret = -ENOMEM; |
| 558 | goto err_kmalloc; |
| 559 | } |
| 560 | |
| 561 | hdq_data->dev = &pdev->dev; |
| 562 | platform_set_drvdata(pdev, hdq_data); |
| 563 | |
| 564 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 565 | if (!res) { |
| 566 | dev_dbg(&pdev->dev, "unable to get resource\n"); |
| 567 | ret = -ENXIO; |
| 568 | goto err_resource; |
| 569 | } |
| 570 | |
| 571 | hdq_data->hdq_base = ioremap(res->start, SZ_4K); |
| 572 | if (!hdq_data->hdq_base) { |
| 573 | dev_dbg(&pdev->dev, "ioremap failed\n"); |
| 574 | ret = -EINVAL; |
| 575 | goto err_ioremap; |
| 576 | } |
| 577 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 578 | hdq_data->hdq_usecount = 0; |
| 579 | mutex_init(&hdq_data->hdq_mutex); |
| 580 | |
Paul Walmsley | c354a86 | 2012-06-21 21:40:40 -0600 | [diff] [blame^] | 581 | pm_runtime_enable(&pdev->dev); |
| 582 | pm_runtime_get_sync(&pdev->dev); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 583 | |
| 584 | rev = hdq_reg_in(hdq_data, OMAP_HDQ_REVISION); |
| 585 | dev_info(&pdev->dev, "OMAP HDQ Hardware Rev %c.%c. Driver in %s mode\n", |
| 586 | (rev >> 4) + '0', (rev & 0x0f) + '0', "Interrupt"); |
| 587 | |
| 588 | spin_lock_init(&hdq_data->hdq_spinlock); |
| 589 | |
| 590 | irq = platform_get_irq(pdev, 0); |
| 591 | if (irq < 0) { |
| 592 | ret = -ENXIO; |
| 593 | goto err_irq; |
| 594 | } |
| 595 | |
| 596 | ret = request_irq(irq, hdq_isr, IRQF_DISABLED, "omap_hdq", hdq_data); |
| 597 | if (ret < 0) { |
| 598 | dev_dbg(&pdev->dev, "could not request irq\n"); |
| 599 | goto err_irq; |
| 600 | } |
| 601 | |
| 602 | omap_hdq_break(hdq_data); |
| 603 | |
Paul Walmsley | c354a86 | 2012-06-21 21:40:40 -0600 | [diff] [blame^] | 604 | pm_runtime_put_sync(&pdev->dev); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 605 | |
| 606 | omap_w1_master.data = hdq_data; |
| 607 | |
| 608 | ret = w1_add_master_device(&omap_w1_master); |
| 609 | if (ret) { |
| 610 | dev_dbg(&pdev->dev, "Failure in registering w1 master\n"); |
| 611 | goto err_w1; |
| 612 | } |
| 613 | |
| 614 | return 0; |
| 615 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 616 | err_irq: |
Paul Walmsley | c354a86 | 2012-06-21 21:40:40 -0600 | [diff] [blame^] | 617 | pm_runtime_put_sync(&pdev->dev); |
| 618 | err_w1: |
| 619 | pm_runtime_disable(&pdev->dev); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 620 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 621 | iounmap(hdq_data->hdq_base); |
| 622 | |
| 623 | err_ioremap: |
| 624 | err_resource: |
| 625 | platform_set_drvdata(pdev, NULL); |
| 626 | kfree(hdq_data); |
| 627 | |
| 628 | err_kmalloc: |
| 629 | return ret; |
| 630 | |
| 631 | } |
| 632 | |
| 633 | static int omap_hdq_remove(struct platform_device *pdev) |
| 634 | { |
| 635 | struct hdq_data *hdq_data = platform_get_drvdata(pdev); |
| 636 | |
| 637 | mutex_lock(&hdq_data->hdq_mutex); |
| 638 | |
| 639 | if (hdq_data->hdq_usecount) { |
| 640 | dev_dbg(&pdev->dev, "removed when use count is not zero\n"); |
Stoyan Gaydarov | 2020002 | 2009-08-06 15:07:28 -0700 | [diff] [blame] | 641 | mutex_unlock(&hdq_data->hdq_mutex); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 642 | return -EBUSY; |
| 643 | } |
| 644 | |
| 645 | mutex_unlock(&hdq_data->hdq_mutex); |
| 646 | |
| 647 | /* remove module dependency */ |
Paul Walmsley | c354a86 | 2012-06-21 21:40:40 -0600 | [diff] [blame^] | 648 | pm_runtime_disable(&pdev->dev); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 649 | free_irq(INT_24XX_HDQ_IRQ, hdq_data); |
| 650 | platform_set_drvdata(pdev, NULL); |
| 651 | iounmap(hdq_data->hdq_base); |
| 652 | kfree(hdq_data); |
| 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | static int __init |
| 658 | omap_hdq_init(void) |
| 659 | { |
| 660 | return platform_driver_register(&omap_hdq_driver); |
| 661 | } |
| 662 | module_init(omap_hdq_init); |
| 663 | |
| 664 | static void __exit |
| 665 | omap_hdq_exit(void) |
| 666 | { |
| 667 | platform_driver_unregister(&omap_hdq_driver); |
| 668 | } |
| 669 | module_exit(omap_hdq_exit); |
| 670 | |
| 671 | module_param(w1_id, int, S_IRUSR); |
| 672 | MODULE_PARM_DESC(w1_id, "1-wire id for the slave detection"); |
| 673 | |
| 674 | MODULE_AUTHOR("Texas Instruments"); |
| 675 | MODULE_DESCRIPTION("HDQ driver Library"); |
| 676 | MODULE_LICENSE("GPL"); |