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> |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 20 | #include <linux/of.h> |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 21 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 22 | #include "../w1.h" |
| 23 | #include "../w1_int.h" |
| 24 | |
| 25 | #define MOD_NAME "OMAP_HDQ:" |
| 26 | |
| 27 | #define OMAP_HDQ_REVISION 0x00 |
| 28 | #define OMAP_HDQ_TX_DATA 0x04 |
| 29 | #define OMAP_HDQ_RX_DATA 0x08 |
| 30 | #define OMAP_HDQ_CTRL_STATUS 0x0c |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 31 | #define OMAP_HDQ_CTRL_STATUS_SINGLE BIT(7) |
| 32 | #define OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK BIT(6) |
| 33 | #define OMAP_HDQ_CTRL_STATUS_CLOCKENABLE BIT(5) |
| 34 | #define OMAP_HDQ_CTRL_STATUS_GO BIT(4) |
| 35 | #define OMAP_HDQ_CTRL_STATUS_PRESENCE BIT(3) |
| 36 | #define OMAP_HDQ_CTRL_STATUS_INITIALIZATION BIT(2) |
| 37 | #define OMAP_HDQ_CTRL_STATUS_DIR BIT(1) |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 38 | #define OMAP_HDQ_INT_STATUS 0x10 |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 39 | #define OMAP_HDQ_INT_STATUS_TXCOMPLETE BIT(2) |
| 40 | #define OMAP_HDQ_INT_STATUS_RXCOMPLETE BIT(1) |
| 41 | #define OMAP_HDQ_INT_STATUS_TIMEOUT BIT(0) |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 42 | #define OMAP_HDQ_SYSCONFIG 0x14 |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 43 | #define OMAP_HDQ_SYSCONFIG_SOFTRESET BIT(1) |
| 44 | #define OMAP_HDQ_SYSCONFIG_AUTOIDLE BIT(0) |
| 45 | #define OMAP_HDQ_SYSCONFIG_NOIDLE 0x0 |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 46 | #define OMAP_HDQ_SYSSTATUS 0x18 |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 47 | #define OMAP_HDQ_SYSSTATUS_RESETDONE BIT(0) |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 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; |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 73 | int rrw; |
| 74 | /* mode: 0-HDQ 1-W1 */ |
| 75 | int mode; |
| 76 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 77 | }; |
| 78 | |
Bill Pemberton | 479e2bc | 2012-11-19 13:21:43 -0500 | [diff] [blame] | 79 | static int omap_hdq_probe(struct platform_device *pdev); |
Bill Pemberton | 82849a9 | 2012-11-19 13:26:23 -0500 | [diff] [blame] | 80 | static int omap_hdq_remove(struct platform_device *pdev); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 81 | |
Fabian Frederick | 0a56c0e | 2015-03-16 20:20:29 +0100 | [diff] [blame] | 82 | static const struct of_device_id omap_hdq_dt_ids[] = { |
NeilBrown | 126e31f | 2014-11-08 10:30:32 +1100 | [diff] [blame] | 83 | { .compatible = "ti,omap3-1w" }, |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 84 | { .compatible = "ti,am4372-hdq" }, |
NeilBrown | 126e31f | 2014-11-08 10:30:32 +1100 | [diff] [blame] | 85 | {} |
| 86 | }; |
| 87 | MODULE_DEVICE_TABLE(of, omap_hdq_dt_ids); |
| 88 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 89 | static struct platform_driver omap_hdq_driver = { |
| 90 | .probe = omap_hdq_probe, |
Bill Pemberton | f91a66c | 2012-11-19 13:21:14 -0500 | [diff] [blame] | 91 | .remove = omap_hdq_remove, |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 92 | .driver = { |
| 93 | .name = "omap_hdq", |
NeilBrown | 126e31f | 2014-11-08 10:30:32 +1100 | [diff] [blame] | 94 | .of_match_table = omap_hdq_dt_ids, |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 95 | }, |
| 96 | }; |
| 97 | |
| 98 | static u8 omap_w1_read_byte(void *_hdq); |
| 99 | static void omap_w1_write_byte(void *_hdq, u8 byte); |
| 100 | static u8 omap_w1_reset_bus(void *_hdq); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 101 | |
| 102 | |
| 103 | static struct w1_bus_master omap_w1_master = { |
| 104 | .read_byte = omap_w1_read_byte, |
| 105 | .write_byte = omap_w1_write_byte, |
| 106 | .reset_bus = omap_w1_reset_bus, |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | /* HDQ register I/O routines */ |
| 110 | static inline u8 hdq_reg_in(struct hdq_data *hdq_data, u32 offset) |
| 111 | { |
Paul Walmsley | 2acd0894 | 2012-06-21 21:40:37 -0600 | [diff] [blame] | 112 | return __raw_readl(hdq_data->hdq_base + offset); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static inline void hdq_reg_out(struct hdq_data *hdq_data, u32 offset, u8 val) |
| 116 | { |
Paul Walmsley | 2acd0894 | 2012-06-21 21:40:37 -0600 | [diff] [blame] | 117 | __raw_writel(val, hdq_data->hdq_base + offset); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static inline u8 hdq_reg_merge(struct hdq_data *hdq_data, u32 offset, |
| 121 | u8 val, u8 mask) |
| 122 | { |
Paul Walmsley | 2acd0894 | 2012-06-21 21:40:37 -0600 | [diff] [blame] | 123 | u8 new_val = (__raw_readl(hdq_data->hdq_base + offset) & ~mask) |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 124 | | (val & mask); |
Paul Walmsley | 2acd0894 | 2012-06-21 21:40:37 -0600 | [diff] [blame] | 125 | __raw_writel(new_val, hdq_data->hdq_base + offset); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 126 | |
| 127 | return new_val; |
| 128 | } |
| 129 | |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 130 | static void hdq_disable_interrupt(struct hdq_data *hdq_data, u32 offset, |
| 131 | u32 mask) |
| 132 | { |
| 133 | u32 ie; |
| 134 | |
| 135 | ie = readl(hdq_data->hdq_base + offset); |
| 136 | writel(ie & mask, hdq_data->hdq_base + offset); |
| 137 | } |
| 138 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 139 | /* |
| 140 | * Wait for one or more bits in flag change. |
| 141 | * HDQ_FLAG_SET: wait until any bit in the flag is set. |
| 142 | * HDQ_FLAG_CLEAR: wait until all bits in the flag are cleared. |
| 143 | * return 0 on success and -ETIMEDOUT in the case of timeout. |
| 144 | */ |
| 145 | static int hdq_wait_for_flag(struct hdq_data *hdq_data, u32 offset, |
| 146 | u8 flag, u8 flag_set, u8 *status) |
| 147 | { |
| 148 | int ret = 0; |
| 149 | unsigned long timeout = jiffies + OMAP_HDQ_TIMEOUT; |
| 150 | |
| 151 | if (flag_set == OMAP_HDQ_FLAG_CLEAR) { |
| 152 | /* wait for the flag clear */ |
| 153 | while (((*status = hdq_reg_in(hdq_data, offset)) & flag) |
| 154 | && time_before(jiffies, timeout)) { |
| 155 | schedule_timeout_uninterruptible(1); |
| 156 | } |
| 157 | if (*status & flag) |
| 158 | ret = -ETIMEDOUT; |
| 159 | } else if (flag_set == OMAP_HDQ_FLAG_SET) { |
| 160 | /* wait for the flag set */ |
| 161 | while (!((*status = hdq_reg_in(hdq_data, offset)) & flag) |
| 162 | && time_before(jiffies, timeout)) { |
| 163 | schedule_timeout_uninterruptible(1); |
| 164 | } |
| 165 | if (!(*status & flag)) |
| 166 | ret = -ETIMEDOUT; |
| 167 | } else |
| 168 | return -EINVAL; |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | /* write out a byte and fill *status with HDQ_INT_STATUS */ |
| 174 | static int hdq_write_byte(struct hdq_data *hdq_data, u8 val, u8 *status) |
| 175 | { |
| 176 | int ret; |
| 177 | u8 tmp_status; |
| 178 | unsigned long irqflags; |
| 179 | |
| 180 | *status = 0; |
| 181 | |
| 182 | spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags); |
| 183 | /* clear interrupt flags via a dummy read */ |
| 184 | hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS); |
| 185 | /* ISR loads it with new INT_STATUS */ |
| 186 | hdq_data->hdq_irqstatus = 0; |
| 187 | spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags); |
| 188 | |
| 189 | hdq_reg_out(hdq_data, OMAP_HDQ_TX_DATA, val); |
| 190 | |
| 191 | /* set the GO bit */ |
| 192 | hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, OMAP_HDQ_CTRL_STATUS_GO, |
| 193 | OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO); |
| 194 | /* wait for the TXCOMPLETE bit */ |
| 195 | ret = wait_event_timeout(hdq_wait_queue, |
| 196 | hdq_data->hdq_irqstatus, OMAP_HDQ_TIMEOUT); |
| 197 | if (ret == 0) { |
| 198 | dev_dbg(hdq_data->dev, "TX wait elapsed\n"); |
NeilBrown | 7b5362a | 2012-05-22 09:43:02 +1000 | [diff] [blame] | 199 | ret = -ETIMEDOUT; |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 200 | goto out; |
| 201 | } |
| 202 | |
| 203 | *status = hdq_data->hdq_irqstatus; |
| 204 | /* check irqstatus */ |
| 205 | if (!(*status & OMAP_HDQ_INT_STATUS_TXCOMPLETE)) { |
| 206 | dev_dbg(hdq_data->dev, "timeout waiting for" |
NeilBrown | 7b5362a | 2012-05-22 09:43:02 +1000 | [diff] [blame] | 207 | " TXCOMPLETE/RXCOMPLETE, %x", *status); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 208 | ret = -ETIMEDOUT; |
| 209 | goto out; |
| 210 | } |
| 211 | |
| 212 | /* wait for the GO bit return to zero */ |
| 213 | ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_CTRL_STATUS, |
| 214 | OMAP_HDQ_CTRL_STATUS_GO, |
| 215 | OMAP_HDQ_FLAG_CLEAR, &tmp_status); |
| 216 | if (ret) { |
| 217 | dev_dbg(hdq_data->dev, "timeout waiting GO bit" |
NeilBrown | 7b5362a | 2012-05-22 09:43:02 +1000 | [diff] [blame] | 218 | " return to zero, %x", tmp_status); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | out: |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | /* HDQ Interrupt service routine */ |
| 226 | static irqreturn_t hdq_isr(int irq, void *_hdq) |
| 227 | { |
| 228 | struct hdq_data *hdq_data = _hdq; |
| 229 | unsigned long irqflags; |
| 230 | |
| 231 | spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags); |
| 232 | hdq_data->hdq_irqstatus = hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS); |
| 233 | spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags); |
| 234 | dev_dbg(hdq_data->dev, "hdq_isr: %x", hdq_data->hdq_irqstatus); |
| 235 | |
| 236 | if (hdq_data->hdq_irqstatus & |
| 237 | (OMAP_HDQ_INT_STATUS_TXCOMPLETE | OMAP_HDQ_INT_STATUS_RXCOMPLETE |
| 238 | | OMAP_HDQ_INT_STATUS_TIMEOUT)) { |
| 239 | /* wake up sleeping process */ |
| 240 | wake_up(&hdq_wait_queue); |
| 241 | } |
| 242 | |
| 243 | return IRQ_HANDLED; |
| 244 | } |
| 245 | |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 246 | /* W1 search callback function in HDQ mode */ |
Stanley.Miao | 06b0d4d | 2008-11-19 15:36:50 -0800 | [diff] [blame] | 247 | static void omap_w1_search_bus(void *_hdq, struct w1_master *master_dev, |
| 248 | u8 search_type, w1_slave_found_callback slave_found) |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 249 | { |
| 250 | u64 module_id, rn_le, cs, id; |
| 251 | |
| 252 | if (w1_id) |
| 253 | module_id = w1_id; |
| 254 | else |
| 255 | module_id = 0x1; |
| 256 | |
| 257 | rn_le = cpu_to_le64(module_id); |
| 258 | /* |
| 259 | * HDQ might not obey truly the 1-wire spec. |
| 260 | * So calculate CRC based on module parameter. |
| 261 | */ |
| 262 | cs = w1_calc_crc8((u8 *)&rn_le, 7); |
| 263 | id = (cs << 56) | module_id; |
| 264 | |
Stanley.Miao | 06b0d4d | 2008-11-19 15:36:50 -0800 | [diff] [blame] | 265 | slave_found(master_dev, id); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static int _omap_hdq_reset(struct hdq_data *hdq_data) |
| 269 | { |
| 270 | int ret; |
| 271 | u8 tmp_status; |
| 272 | |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 273 | hdq_reg_out(hdq_data, OMAP_HDQ_SYSCONFIG, |
| 274 | OMAP_HDQ_SYSCONFIG_SOFTRESET); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 275 | /* |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 276 | * Select HDQ/1W mode & enable clocks. |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 277 | * It is observed that INT flags can't be cleared via a read and GO/INIT |
| 278 | * won't return to zero if interrupt is disabled. So we always enable |
| 279 | * interrupt. |
| 280 | */ |
| 281 | hdq_reg_out(hdq_data, OMAP_HDQ_CTRL_STATUS, |
| 282 | OMAP_HDQ_CTRL_STATUS_CLOCKENABLE | |
| 283 | OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK); |
| 284 | |
| 285 | /* wait for reset to complete */ |
| 286 | ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_SYSSTATUS, |
| 287 | OMAP_HDQ_SYSSTATUS_RESETDONE, OMAP_HDQ_FLAG_SET, &tmp_status); |
| 288 | if (ret) |
| 289 | dev_dbg(hdq_data->dev, "timeout waiting HDQ reset, %x", |
| 290 | tmp_status); |
| 291 | else { |
| 292 | hdq_reg_out(hdq_data, OMAP_HDQ_CTRL_STATUS, |
| 293 | OMAP_HDQ_CTRL_STATUS_CLOCKENABLE | |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 294 | OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK | |
| 295 | hdq_data->mode); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 296 | hdq_reg_out(hdq_data, OMAP_HDQ_SYSCONFIG, |
| 297 | OMAP_HDQ_SYSCONFIG_AUTOIDLE); |
| 298 | } |
| 299 | |
| 300 | return ret; |
| 301 | } |
| 302 | |
| 303 | /* Issue break pulse to the device */ |
| 304 | static int omap_hdq_break(struct hdq_data *hdq_data) |
| 305 | { |
| 306 | int ret = 0; |
| 307 | u8 tmp_status; |
| 308 | unsigned long irqflags; |
| 309 | |
| 310 | ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); |
| 311 | if (ret < 0) { |
| 312 | dev_dbg(hdq_data->dev, "Could not acquire mutex\n"); |
| 313 | ret = -EINTR; |
| 314 | goto rtn; |
| 315 | } |
| 316 | |
| 317 | spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags); |
| 318 | /* clear interrupt flags via a dummy read */ |
| 319 | hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS); |
| 320 | /* ISR loads it with new INT_STATUS */ |
| 321 | hdq_data->hdq_irqstatus = 0; |
| 322 | spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags); |
| 323 | |
| 324 | /* set the INIT and GO bit */ |
| 325 | hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, |
| 326 | OMAP_HDQ_CTRL_STATUS_INITIALIZATION | OMAP_HDQ_CTRL_STATUS_GO, |
| 327 | OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_INITIALIZATION | |
| 328 | OMAP_HDQ_CTRL_STATUS_GO); |
| 329 | |
| 330 | /* wait for the TIMEOUT bit */ |
| 331 | ret = wait_event_timeout(hdq_wait_queue, |
| 332 | hdq_data->hdq_irqstatus, OMAP_HDQ_TIMEOUT); |
| 333 | if (ret == 0) { |
| 334 | dev_dbg(hdq_data->dev, "break wait elapsed\n"); |
| 335 | ret = -EINTR; |
| 336 | goto out; |
| 337 | } |
| 338 | |
| 339 | tmp_status = hdq_data->hdq_irqstatus; |
| 340 | /* check irqstatus */ |
| 341 | if (!(tmp_status & OMAP_HDQ_INT_STATUS_TIMEOUT)) { |
| 342 | dev_dbg(hdq_data->dev, "timeout waiting for TIMEOUT, %x", |
| 343 | tmp_status); |
| 344 | ret = -ETIMEDOUT; |
| 345 | goto out; |
| 346 | } |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 347 | |
| 348 | /* |
| 349 | * check for the presence detect bit to get |
| 350 | * set to show that the slave is responding |
| 351 | */ |
| 352 | if (!(hdq_reg_in(hdq_data, OMAP_HDQ_CTRL_STATUS) & |
| 353 | OMAP_HDQ_CTRL_STATUS_PRESENCE)) { |
| 354 | dev_dbg(hdq_data->dev, "Presence bit not set\n"); |
| 355 | ret = -ETIMEDOUT; |
| 356 | goto out; |
| 357 | } |
| 358 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 359 | /* |
| 360 | * wait for both INIT and GO bits rerurn to zero. |
| 361 | * zero wait time expected for interrupt mode. |
| 362 | */ |
| 363 | ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_CTRL_STATUS, |
| 364 | OMAP_HDQ_CTRL_STATUS_INITIALIZATION | |
| 365 | OMAP_HDQ_CTRL_STATUS_GO, OMAP_HDQ_FLAG_CLEAR, |
| 366 | &tmp_status); |
| 367 | if (ret) |
| 368 | dev_dbg(hdq_data->dev, "timeout waiting INIT&GO bits" |
NeilBrown | 7b5362a | 2012-05-22 09:43:02 +1000 | [diff] [blame] | 369 | " return to zero, %x", tmp_status); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 370 | |
| 371 | out: |
| 372 | mutex_unlock(&hdq_data->hdq_mutex); |
| 373 | rtn: |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | static int hdq_read_byte(struct hdq_data *hdq_data, u8 *val) |
| 378 | { |
| 379 | int ret = 0; |
| 380 | u8 status; |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 381 | |
| 382 | ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); |
| 383 | if (ret < 0) { |
| 384 | ret = -EINTR; |
| 385 | goto rtn; |
| 386 | } |
| 387 | |
| 388 | if (!hdq_data->hdq_usecount) { |
| 389 | ret = -EINVAL; |
| 390 | goto out; |
| 391 | } |
| 392 | |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 393 | hdq_data->hdq_irqstatus = 0; |
| 394 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 395 | if (!(hdq_data->hdq_irqstatus & OMAP_HDQ_INT_STATUS_RXCOMPLETE)) { |
| 396 | hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, |
| 397 | OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO, |
| 398 | OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO); |
| 399 | /* |
NeilBrown | b7e938d | 2012-05-22 09:43:02 +1000 | [diff] [blame] | 400 | * The RX comes immediately after TX. |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 401 | */ |
NeilBrown | b7e938d | 2012-05-22 09:43:02 +1000 | [diff] [blame] | 402 | wait_event_timeout(hdq_wait_queue, |
| 403 | (hdq_data->hdq_irqstatus |
| 404 | & OMAP_HDQ_INT_STATUS_RXCOMPLETE), |
| 405 | OMAP_HDQ_TIMEOUT); |
| 406 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 407 | hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, 0, |
| 408 | OMAP_HDQ_CTRL_STATUS_DIR); |
| 409 | status = hdq_data->hdq_irqstatus; |
| 410 | /* check irqstatus */ |
| 411 | if (!(status & OMAP_HDQ_INT_STATUS_RXCOMPLETE)) { |
| 412 | dev_dbg(hdq_data->dev, "timeout waiting for" |
NeilBrown | 7b5362a | 2012-05-22 09:43:02 +1000 | [diff] [blame] | 413 | " RXCOMPLETE, %x", status); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 414 | ret = -ETIMEDOUT; |
| 415 | goto out; |
| 416 | } |
| 417 | } |
| 418 | /* the data is ready. Read it in! */ |
| 419 | *val = hdq_reg_in(hdq_data, OMAP_HDQ_RX_DATA); |
| 420 | out: |
| 421 | mutex_unlock(&hdq_data->hdq_mutex); |
| 422 | rtn: |
NeilBrown | 7b5362a | 2012-05-22 09:43:02 +1000 | [diff] [blame] | 423 | return ret; |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 424 | |
| 425 | } |
| 426 | |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 427 | /* Enable clocks and set the controller to HDQ/1W mode */ |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 428 | static int omap_hdq_get(struct hdq_data *hdq_data) |
| 429 | { |
| 430 | int ret = 0; |
| 431 | |
| 432 | ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); |
| 433 | if (ret < 0) { |
| 434 | ret = -EINTR; |
| 435 | goto rtn; |
| 436 | } |
| 437 | |
| 438 | if (OMAP_HDQ_MAX_USER == hdq_data->hdq_usecount) { |
| 439 | dev_dbg(hdq_data->dev, "attempt to exceed the max use count"); |
| 440 | ret = -EINVAL; |
| 441 | goto out; |
| 442 | } else { |
| 443 | hdq_data->hdq_usecount++; |
| 444 | try_module_get(THIS_MODULE); |
| 445 | if (1 == hdq_data->hdq_usecount) { |
Paul Walmsley | c354a86 | 2012-06-21 21:40:40 -0600 | [diff] [blame] | 446 | |
| 447 | pm_runtime_get_sync(hdq_data->dev); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 448 | |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 449 | /* make sure HDQ/1W is out of reset */ |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 450 | if (!(hdq_reg_in(hdq_data, OMAP_HDQ_SYSSTATUS) & |
| 451 | OMAP_HDQ_SYSSTATUS_RESETDONE)) { |
| 452 | ret = _omap_hdq_reset(hdq_data); |
| 453 | if (ret) |
| 454 | /* back up the count */ |
| 455 | hdq_data->hdq_usecount--; |
| 456 | } else { |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 457 | /* select HDQ/1W mode & enable clocks */ |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 458 | hdq_reg_out(hdq_data, OMAP_HDQ_CTRL_STATUS, |
| 459 | OMAP_HDQ_CTRL_STATUS_CLOCKENABLE | |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 460 | OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK | |
| 461 | hdq_data->mode); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 462 | hdq_reg_out(hdq_data, OMAP_HDQ_SYSCONFIG, |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 463 | OMAP_HDQ_SYSCONFIG_NOIDLE); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 464 | hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS); |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 469 | out: |
| 470 | mutex_unlock(&hdq_data->hdq_mutex); |
| 471 | rtn: |
| 472 | return ret; |
| 473 | } |
| 474 | |
| 475 | /* Disable clocks to the module */ |
| 476 | static int omap_hdq_put(struct hdq_data *hdq_data) |
| 477 | { |
| 478 | int ret = 0; |
| 479 | |
| 480 | ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); |
| 481 | if (ret < 0) |
| 482 | return -EINTR; |
| 483 | |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 484 | hdq_reg_out(hdq_data, OMAP_HDQ_SYSCONFIG, |
| 485 | OMAP_HDQ_SYSCONFIG_AUTOIDLE); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 486 | if (0 == hdq_data->hdq_usecount) { |
| 487 | dev_dbg(hdq_data->dev, "attempt to decrement use count" |
NeilBrown | 7b5362a | 2012-05-22 09:43:02 +1000 | [diff] [blame] | 488 | " when it is zero"); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 489 | ret = -EINVAL; |
| 490 | } else { |
| 491 | hdq_data->hdq_usecount--; |
| 492 | module_put(THIS_MODULE); |
Paul Walmsley | c354a86 | 2012-06-21 21:40:40 -0600 | [diff] [blame] | 493 | if (0 == hdq_data->hdq_usecount) |
| 494 | pm_runtime_put_sync(hdq_data->dev); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 495 | } |
| 496 | mutex_unlock(&hdq_data->hdq_mutex); |
| 497 | |
| 498 | return ret; |
| 499 | } |
| 500 | |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 501 | /* |
| 502 | * W1 triplet callback function - used for searching ROM addresses. |
| 503 | * Registered only when controller is in 1-wire mode. |
| 504 | */ |
| 505 | static u8 omap_w1_triplet(void *_hdq, u8 bdir) |
| 506 | { |
| 507 | u8 id_bit, comp_bit; |
| 508 | int err; |
| 509 | u8 ret = 0x3; /* no slaves responded */ |
| 510 | struct hdq_data *hdq_data = _hdq; |
| 511 | u8 ctrl = OMAP_HDQ_CTRL_STATUS_SINGLE | OMAP_HDQ_CTRL_STATUS_GO | |
| 512 | OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK; |
| 513 | u8 mask = ctrl | OMAP_HDQ_CTRL_STATUS_DIR; |
| 514 | |
| 515 | omap_hdq_get(_hdq); |
| 516 | |
| 517 | err = mutex_lock_interruptible(&hdq_data->hdq_mutex); |
| 518 | if (err < 0) { |
| 519 | dev_dbg(hdq_data->dev, "Could not acquire mutex\n"); |
| 520 | goto rtn; |
| 521 | } |
| 522 | |
| 523 | hdq_data->hdq_irqstatus = 0; |
| 524 | /* read id_bit */ |
| 525 | hdq_reg_merge(_hdq, OMAP_HDQ_CTRL_STATUS, |
| 526 | ctrl | OMAP_HDQ_CTRL_STATUS_DIR, mask); |
| 527 | err = wait_event_timeout(hdq_wait_queue, |
| 528 | (hdq_data->hdq_irqstatus |
| 529 | & OMAP_HDQ_INT_STATUS_RXCOMPLETE), |
| 530 | OMAP_HDQ_TIMEOUT); |
| 531 | if (err == 0) { |
| 532 | dev_dbg(hdq_data->dev, "RX wait elapsed\n"); |
| 533 | goto out; |
| 534 | } |
| 535 | id_bit = (hdq_reg_in(_hdq, OMAP_HDQ_RX_DATA) & 0x01); |
| 536 | |
| 537 | hdq_data->hdq_irqstatus = 0; |
| 538 | /* read comp_bit */ |
| 539 | hdq_reg_merge(_hdq, OMAP_HDQ_CTRL_STATUS, |
| 540 | ctrl | OMAP_HDQ_CTRL_STATUS_DIR, mask); |
| 541 | err = wait_event_timeout(hdq_wait_queue, |
| 542 | (hdq_data->hdq_irqstatus |
| 543 | & OMAP_HDQ_INT_STATUS_RXCOMPLETE), |
| 544 | OMAP_HDQ_TIMEOUT); |
| 545 | if (err == 0) { |
| 546 | dev_dbg(hdq_data->dev, "RX wait elapsed\n"); |
| 547 | goto out; |
| 548 | } |
| 549 | comp_bit = (hdq_reg_in(_hdq, OMAP_HDQ_RX_DATA) & 0x01); |
| 550 | |
| 551 | if (id_bit && comp_bit) { |
| 552 | ret = 0x03; /* no slaves responded */ |
| 553 | goto out; |
| 554 | } |
| 555 | if (!id_bit && !comp_bit) { |
| 556 | /* Both bits are valid, take the direction given */ |
| 557 | ret = bdir ? 0x04 : 0; |
| 558 | } else { |
| 559 | /* Only one bit is valid, take that direction */ |
| 560 | bdir = id_bit; |
| 561 | ret = id_bit ? 0x05 : 0x02; |
| 562 | } |
| 563 | |
| 564 | /* write bdir bit */ |
| 565 | hdq_reg_out(_hdq, OMAP_HDQ_TX_DATA, bdir); |
| 566 | hdq_reg_merge(_hdq, OMAP_HDQ_CTRL_STATUS, ctrl, mask); |
| 567 | err = wait_event_timeout(hdq_wait_queue, |
| 568 | (hdq_data->hdq_irqstatus |
| 569 | & OMAP_HDQ_INT_STATUS_TXCOMPLETE), |
| 570 | OMAP_HDQ_TIMEOUT); |
| 571 | if (err == 0) { |
| 572 | dev_dbg(hdq_data->dev, "TX wait elapsed\n"); |
| 573 | goto out; |
| 574 | } |
| 575 | |
| 576 | hdq_reg_merge(_hdq, OMAP_HDQ_CTRL_STATUS, 0, |
| 577 | OMAP_HDQ_CTRL_STATUS_SINGLE); |
| 578 | |
| 579 | out: |
| 580 | mutex_unlock(&hdq_data->hdq_mutex); |
| 581 | rtn: |
| 582 | omap_hdq_put(_hdq); |
| 583 | return ret; |
| 584 | } |
| 585 | |
| 586 | /* reset callback */ |
| 587 | static u8 omap_w1_reset_bus(void *_hdq) |
| 588 | { |
| 589 | omap_hdq_get(_hdq); |
| 590 | omap_hdq_break(_hdq); |
| 591 | omap_hdq_put(_hdq); |
| 592 | return 0; |
| 593 | } |
| 594 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 595 | /* Read a byte of data from the device */ |
| 596 | static u8 omap_w1_read_byte(void *_hdq) |
| 597 | { |
| 598 | struct hdq_data *hdq_data = _hdq; |
| 599 | u8 val = 0; |
| 600 | int ret; |
| 601 | |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 602 | /* First write to initialize the transfer */ |
| 603 | if (hdq_data->init_trans == 0) |
| 604 | omap_hdq_get(hdq_data); |
| 605 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 606 | ret = hdq_read_byte(hdq_data, &val); |
| 607 | if (ret) { |
| 608 | ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); |
| 609 | if (ret < 0) { |
| 610 | dev_dbg(hdq_data->dev, "Could not acquire mutex\n"); |
| 611 | return -EINTR; |
| 612 | } |
| 613 | hdq_data->init_trans = 0; |
| 614 | mutex_unlock(&hdq_data->hdq_mutex); |
| 615 | omap_hdq_put(hdq_data); |
| 616 | return -1; |
| 617 | } |
| 618 | |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 619 | hdq_disable_interrupt(hdq_data, OMAP_HDQ_CTRL_STATUS, |
| 620 | ~OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK); |
| 621 | hdq_data->hdq_usecount = 0; |
| 622 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 623 | /* Write followed by a read, release the module */ |
| 624 | if (hdq_data->init_trans) { |
| 625 | ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); |
| 626 | if (ret < 0) { |
| 627 | dev_dbg(hdq_data->dev, "Could not acquire mutex\n"); |
| 628 | return -EINTR; |
| 629 | } |
| 630 | hdq_data->init_trans = 0; |
| 631 | mutex_unlock(&hdq_data->hdq_mutex); |
| 632 | omap_hdq_put(hdq_data); |
| 633 | } |
| 634 | |
| 635 | return val; |
| 636 | } |
| 637 | |
| 638 | /* Write a byte of data to the device */ |
| 639 | static void omap_w1_write_byte(void *_hdq, u8 byte) |
| 640 | { |
| 641 | struct hdq_data *hdq_data = _hdq; |
| 642 | int ret; |
| 643 | u8 status; |
| 644 | |
| 645 | /* First write to initialize the transfer */ |
| 646 | if (hdq_data->init_trans == 0) |
| 647 | omap_hdq_get(hdq_data); |
| 648 | |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 649 | /* |
| 650 | * We need to reset the slave before |
| 651 | * issuing the SKIP ROM command, else |
| 652 | * the slave will not work. |
| 653 | */ |
| 654 | if (byte == W1_SKIP_ROM) |
| 655 | omap_hdq_break(hdq_data); |
| 656 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 657 | ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); |
| 658 | if (ret < 0) { |
| 659 | dev_dbg(hdq_data->dev, "Could not acquire mutex\n"); |
| 660 | return; |
| 661 | } |
| 662 | hdq_data->init_trans++; |
| 663 | mutex_unlock(&hdq_data->hdq_mutex); |
| 664 | |
| 665 | ret = hdq_write_byte(hdq_data, byte, &status); |
NeilBrown | 7b5362a | 2012-05-22 09:43:02 +1000 | [diff] [blame] | 666 | if (ret < 0) { |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 667 | dev_dbg(hdq_data->dev, "TX failure:Ctrl status %x\n", status); |
| 668 | return; |
| 669 | } |
| 670 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 671 | /* Second write, data transferred. Release the module */ |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 672 | if (hdq_data->init_trans > 1) { |
| 673 | omap_hdq_put(hdq_data); |
| 674 | ret = mutex_lock_interruptible(&hdq_data->hdq_mutex); |
| 675 | if (ret < 0) { |
| 676 | dev_dbg(hdq_data->dev, "Could not acquire mutex\n"); |
| 677 | return; |
| 678 | } |
| 679 | hdq_data->init_trans = 0; |
| 680 | mutex_unlock(&hdq_data->hdq_mutex); |
| 681 | } |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Bill Pemberton | 479e2bc | 2012-11-19 13:21:43 -0500 | [diff] [blame] | 684 | static int omap_hdq_probe(struct platform_device *pdev) |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 685 | { |
Felipe Balbi | 19afea5 | 2012-07-25 15:05:30 +0300 | [diff] [blame] | 686 | struct device *dev = &pdev->dev; |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 687 | struct hdq_data *hdq_data; |
| 688 | struct resource *res; |
| 689 | int ret, irq; |
| 690 | u8 rev; |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 691 | const char *mode; |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 692 | |
Felipe Balbi | 19afea5 | 2012-07-25 15:05:30 +0300 | [diff] [blame] | 693 | hdq_data = devm_kzalloc(dev, sizeof(*hdq_data), GFP_KERNEL); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 694 | if (!hdq_data) { |
| 695 | dev_dbg(&pdev->dev, "unable to allocate memory\n"); |
Felipe Balbi | 19afea5 | 2012-07-25 15:05:30 +0300 | [diff] [blame] | 696 | return -ENOMEM; |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 697 | } |
| 698 | |
Felipe Balbi | 19afea5 | 2012-07-25 15:05:30 +0300 | [diff] [blame] | 699 | hdq_data->dev = dev; |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 700 | platform_set_drvdata(pdev, hdq_data); |
| 701 | |
| 702 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Thierry Reding | 4d6dc3a | 2013-01-21 11:09:24 +0100 | [diff] [blame] | 703 | hdq_data->hdq_base = devm_ioremap_resource(dev, res); |
| 704 | if (IS_ERR(hdq_data->hdq_base)) |
| 705 | return PTR_ERR(hdq_data->hdq_base); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 706 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 707 | hdq_data->hdq_usecount = 0; |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 708 | hdq_data->rrw = 0; |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 709 | mutex_init(&hdq_data->hdq_mutex); |
| 710 | |
Paul Walmsley | c354a86 | 2012-06-21 21:40:40 -0600 | [diff] [blame] | 711 | pm_runtime_enable(&pdev->dev); |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 712 | ret = pm_runtime_get_sync(&pdev->dev); |
| 713 | if (ret < 0) { |
| 714 | dev_dbg(&pdev->dev, "pm_runtime_get_sync failed\n"); |
| 715 | goto err_w1; |
| 716 | } |
| 717 | |
| 718 | ret = _omap_hdq_reset(hdq_data); |
| 719 | if (ret) { |
| 720 | dev_dbg(&pdev->dev, "reset failed\n"); |
| 721 | return -EINVAL; |
| 722 | } |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 723 | |
| 724 | rev = hdq_reg_in(hdq_data, OMAP_HDQ_REVISION); |
| 725 | dev_info(&pdev->dev, "OMAP HDQ Hardware Rev %c.%c. Driver in %s mode\n", |
| 726 | (rev >> 4) + '0', (rev & 0x0f) + '0', "Interrupt"); |
| 727 | |
| 728 | spin_lock_init(&hdq_data->hdq_spinlock); |
| 729 | |
| 730 | irq = platform_get_irq(pdev, 0); |
| 731 | if (irq < 0) { |
| 732 | ret = -ENXIO; |
| 733 | goto err_irq; |
| 734 | } |
| 735 | |
Michael Opdenacker | fe576a5 | 2013-10-13 09:20:05 +0200 | [diff] [blame] | 736 | ret = devm_request_irq(dev, irq, hdq_isr, 0, "omap_hdq", hdq_data); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 737 | if (ret < 0) { |
| 738 | dev_dbg(&pdev->dev, "could not request irq\n"); |
| 739 | goto err_irq; |
| 740 | } |
| 741 | |
| 742 | omap_hdq_break(hdq_data); |
| 743 | |
Paul Walmsley | c354a86 | 2012-06-21 21:40:40 -0600 | [diff] [blame] | 744 | pm_runtime_put_sync(&pdev->dev); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 745 | |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 746 | ret = of_property_read_string(pdev->dev.of_node, "ti,mode", &mode); |
| 747 | if (ret < 0 || !strcmp(mode, "hdq")) { |
| 748 | hdq_data->mode = 0; |
| 749 | omap_w1_master.search = omap_w1_search_bus; |
| 750 | } else { |
| 751 | hdq_data->mode = 1; |
| 752 | omap_w1_master.triplet = omap_w1_triplet; |
| 753 | } |
| 754 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 755 | omap_w1_master.data = hdq_data; |
| 756 | |
| 757 | ret = w1_add_master_device(&omap_w1_master); |
| 758 | if (ret) { |
| 759 | dev_dbg(&pdev->dev, "Failure in registering w1 master\n"); |
| 760 | goto err_w1; |
| 761 | } |
| 762 | |
| 763 | return 0; |
| 764 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 765 | err_irq: |
Paul Walmsley | c354a86 | 2012-06-21 21:40:40 -0600 | [diff] [blame] | 766 | pm_runtime_put_sync(&pdev->dev); |
| 767 | err_w1: |
| 768 | pm_runtime_disable(&pdev->dev); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 769 | |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 770 | return ret; |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 771 | } |
| 772 | |
Bill Pemberton | 82849a9 | 2012-11-19 13:26:23 -0500 | [diff] [blame] | 773 | static int omap_hdq_remove(struct platform_device *pdev) |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 774 | { |
| 775 | struct hdq_data *hdq_data = platform_get_drvdata(pdev); |
| 776 | |
| 777 | mutex_lock(&hdq_data->hdq_mutex); |
| 778 | |
| 779 | if (hdq_data->hdq_usecount) { |
| 780 | dev_dbg(&pdev->dev, "removed when use count is not zero\n"); |
Stoyan Gaydarov | 2020002 | 2009-08-06 15:07:28 -0700 | [diff] [blame] | 781 | mutex_unlock(&hdq_data->hdq_mutex); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 782 | return -EBUSY; |
| 783 | } |
| 784 | |
| 785 | mutex_unlock(&hdq_data->hdq_mutex); |
| 786 | |
| 787 | /* remove module dependency */ |
Paul Walmsley | c354a86 | 2012-06-21 21:40:40 -0600 | [diff] [blame] | 788 | pm_runtime_disable(&pdev->dev); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 789 | |
| 790 | return 0; |
| 791 | } |
| 792 | |
Felipe Balbi | 8650bbb | 2012-07-25 15:05:29 +0300 | [diff] [blame] | 793 | module_platform_driver(omap_hdq_driver); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 794 | |
| 795 | module_param(w1_id, int, S_IRUSR); |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 796 | MODULE_PARM_DESC(w1_id, "1-wire id for the slave detection in HDQ mode"); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 797 | |
| 798 | MODULE_AUTHOR("Texas Instruments"); |
Vignesh R | e93762b | 2015-09-14 10:54:33 -0700 | [diff] [blame^] | 799 | MODULE_DESCRIPTION("HDQ-1W driver Library"); |
Madhusudhan Chikkature | 9f2bc79 | 2008-11-12 13:27:09 -0800 | [diff] [blame] | 800 | MODULE_LICENSE("GPL"); |