Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com> |
| 3 | * |
| 4 | * Original author: |
| 5 | * Ben Collins <bcollins@ubuntu.com> |
| 6 | * |
| 7 | * Additional work by: |
| 8 | * John Brooks <john.brooks@bluecherry.net> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | */ |
| 20 | |
| 21 | /* XXX: The SOLO6x10 i2c does not have separate interrupts for each i2c |
| 22 | * channel. The bus can only handle one i2c event at a time. The below handles |
| 23 | * this all wrong. We should be using the status registers to see if the bus |
| 24 | * is in use, and have a global lock to check the status register. Also, |
| 25 | * the bulk of the work should be handled out-of-interrupt. The ugly loops |
| 26 | * that occur during interrupt scare me. The ISR should merely signal |
| 27 | * thread context, ACK the interrupt, and move on. -- BenC */ |
| 28 | |
| 29 | #include <linux/kernel.h> |
| 30 | |
| 31 | #include "solo6x10.h" |
| 32 | |
| 33 | u8 solo_i2c_readbyte(struct solo_dev *solo_dev, int id, u8 addr, u8 off) |
| 34 | { |
| 35 | struct i2c_msg msgs[2]; |
| 36 | u8 data; |
| 37 | |
| 38 | msgs[0].flags = 0; |
| 39 | msgs[0].addr = addr; |
| 40 | msgs[0].len = 1; |
| 41 | msgs[0].buf = &off; |
| 42 | |
| 43 | msgs[1].flags = I2C_M_RD; |
| 44 | msgs[1].addr = addr; |
| 45 | msgs[1].len = 1; |
| 46 | msgs[1].buf = &data; |
| 47 | |
| 48 | i2c_transfer(&solo_dev->i2c_adap[id], msgs, 2); |
| 49 | |
| 50 | return data; |
| 51 | } |
| 52 | |
| 53 | void solo_i2c_writebyte(struct solo_dev *solo_dev, int id, u8 addr, |
| 54 | u8 off, u8 data) |
| 55 | { |
| 56 | struct i2c_msg msgs; |
| 57 | u8 buf[2]; |
| 58 | |
| 59 | buf[0] = off; |
| 60 | buf[1] = data; |
| 61 | msgs.flags = 0; |
| 62 | msgs.addr = addr; |
| 63 | msgs.len = 2; |
| 64 | msgs.buf = buf; |
| 65 | |
| 66 | i2c_transfer(&solo_dev->i2c_adap[id], &msgs, 1); |
| 67 | } |
| 68 | |
| 69 | static void solo_i2c_flush(struct solo_dev *solo_dev, int wr) |
| 70 | { |
| 71 | u32 ctrl; |
| 72 | |
| 73 | ctrl = SOLO_IIC_CH_SET(solo_dev->i2c_id); |
| 74 | |
| 75 | if (solo_dev->i2c_state == IIC_STATE_START) |
| 76 | ctrl |= SOLO_IIC_START; |
| 77 | |
| 78 | if (wr) { |
| 79 | ctrl |= SOLO_IIC_WRITE; |
| 80 | } else { |
| 81 | ctrl |= SOLO_IIC_READ; |
| 82 | if (!(solo_dev->i2c_msg->flags & I2C_M_NO_RD_ACK)) |
| 83 | ctrl |= SOLO_IIC_ACK_EN; |
| 84 | } |
| 85 | |
| 86 | if (solo_dev->i2c_msg_ptr == solo_dev->i2c_msg->len) |
| 87 | ctrl |= SOLO_IIC_STOP; |
| 88 | |
| 89 | solo_reg_write(solo_dev, SOLO_IIC_CTRL, ctrl); |
| 90 | } |
| 91 | |
| 92 | static void solo_i2c_start(struct solo_dev *solo_dev) |
| 93 | { |
| 94 | u32 addr = solo_dev->i2c_msg->addr << 1; |
| 95 | |
| 96 | if (solo_dev->i2c_msg->flags & I2C_M_RD) |
| 97 | addr |= 1; |
| 98 | |
| 99 | solo_dev->i2c_state = IIC_STATE_START; |
| 100 | solo_reg_write(solo_dev, SOLO_IIC_TXD, addr); |
| 101 | solo_i2c_flush(solo_dev, 1); |
| 102 | } |
| 103 | |
| 104 | static void solo_i2c_stop(struct solo_dev *solo_dev) |
| 105 | { |
| 106 | solo_irq_off(solo_dev, SOLO_IRQ_IIC); |
| 107 | solo_reg_write(solo_dev, SOLO_IIC_CTRL, 0); |
| 108 | solo_dev->i2c_state = IIC_STATE_STOP; |
| 109 | wake_up(&solo_dev->i2c_wait); |
| 110 | } |
| 111 | |
| 112 | static int solo_i2c_handle_read(struct solo_dev *solo_dev) |
| 113 | { |
| 114 | prepare_read: |
| 115 | if (solo_dev->i2c_msg_ptr != solo_dev->i2c_msg->len) { |
| 116 | solo_i2c_flush(solo_dev, 0); |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | solo_dev->i2c_msg_ptr = 0; |
| 121 | solo_dev->i2c_msg++; |
| 122 | solo_dev->i2c_msg_num--; |
| 123 | |
| 124 | if (solo_dev->i2c_msg_num == 0) { |
| 125 | solo_i2c_stop(solo_dev); |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | if (!(solo_dev->i2c_msg->flags & I2C_M_NOSTART)) { |
| 130 | solo_i2c_start(solo_dev); |
| 131 | } else { |
| 132 | if (solo_dev->i2c_msg->flags & I2C_M_RD) |
| 133 | goto prepare_read; |
| 134 | else |
| 135 | solo_i2c_stop(solo_dev); |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static int solo_i2c_handle_write(struct solo_dev *solo_dev) |
| 142 | { |
| 143 | retry_write: |
| 144 | if (solo_dev->i2c_msg_ptr != solo_dev->i2c_msg->len) { |
| 145 | solo_reg_write(solo_dev, SOLO_IIC_TXD, |
| 146 | solo_dev->i2c_msg->buf[solo_dev->i2c_msg_ptr]); |
| 147 | solo_dev->i2c_msg_ptr++; |
| 148 | solo_i2c_flush(solo_dev, 1); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | solo_dev->i2c_msg_ptr = 0; |
| 153 | solo_dev->i2c_msg++; |
| 154 | solo_dev->i2c_msg_num--; |
| 155 | |
| 156 | if (solo_dev->i2c_msg_num == 0) { |
| 157 | solo_i2c_stop(solo_dev); |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | if (!(solo_dev->i2c_msg->flags & I2C_M_NOSTART)) { |
| 162 | solo_i2c_start(solo_dev); |
| 163 | } else { |
| 164 | if (solo_dev->i2c_msg->flags & I2C_M_RD) |
| 165 | solo_i2c_stop(solo_dev); |
| 166 | else |
| 167 | goto retry_write; |
| 168 | } |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | int solo_i2c_isr(struct solo_dev *solo_dev) |
| 174 | { |
| 175 | u32 status = solo_reg_read(solo_dev, SOLO_IIC_CTRL); |
| 176 | int ret = -EINVAL; |
| 177 | |
| 178 | |
| 179 | if (CHK_FLAGS(status, SOLO_IIC_STATE_TRNS | SOLO_IIC_STATE_SIG_ERR) |
| 180 | || solo_dev->i2c_id < 0) { |
| 181 | solo_i2c_stop(solo_dev); |
| 182 | return -ENXIO; |
| 183 | } |
| 184 | |
| 185 | switch (solo_dev->i2c_state) { |
| 186 | case IIC_STATE_START: |
| 187 | if (solo_dev->i2c_msg->flags & I2C_M_RD) { |
| 188 | solo_dev->i2c_state = IIC_STATE_READ; |
| 189 | ret = solo_i2c_handle_read(solo_dev); |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | solo_dev->i2c_state = IIC_STATE_WRITE; |
| 194 | case IIC_STATE_WRITE: |
| 195 | ret = solo_i2c_handle_write(solo_dev); |
| 196 | break; |
| 197 | |
| 198 | case IIC_STATE_READ: |
| 199 | solo_dev->i2c_msg->buf[solo_dev->i2c_msg_ptr] = |
| 200 | solo_reg_read(solo_dev, SOLO_IIC_RXD); |
| 201 | solo_dev->i2c_msg_ptr++; |
| 202 | |
| 203 | ret = solo_i2c_handle_read(solo_dev); |
| 204 | break; |
| 205 | |
| 206 | default: |
| 207 | solo_i2c_stop(solo_dev); |
| 208 | } |
| 209 | |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | static int solo_i2c_master_xfer(struct i2c_adapter *adap, |
| 214 | struct i2c_msg msgs[], int num) |
| 215 | { |
| 216 | struct solo_dev *solo_dev = adap->algo_data; |
| 217 | unsigned long timeout; |
| 218 | int ret; |
| 219 | int i; |
| 220 | DEFINE_WAIT(wait); |
| 221 | |
| 222 | for (i = 0; i < SOLO_I2C_ADAPTERS; i++) { |
| 223 | if (&solo_dev->i2c_adap[i] == adap) |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | if (i == SOLO_I2C_ADAPTERS) |
| 228 | return num; /* XXX Right return value for failure? */ |
| 229 | |
| 230 | mutex_lock(&solo_dev->i2c_mutex); |
| 231 | solo_dev->i2c_id = i; |
| 232 | solo_dev->i2c_msg = msgs; |
| 233 | solo_dev->i2c_msg_num = num; |
| 234 | solo_dev->i2c_msg_ptr = 0; |
| 235 | |
| 236 | solo_reg_write(solo_dev, SOLO_IIC_CTRL, 0); |
| 237 | solo_irq_on(solo_dev, SOLO_IRQ_IIC); |
| 238 | solo_i2c_start(solo_dev); |
| 239 | |
| 240 | timeout = HZ / 2; |
| 241 | |
| 242 | for (;;) { |
| 243 | prepare_to_wait(&solo_dev->i2c_wait, &wait, |
| 244 | TASK_INTERRUPTIBLE); |
| 245 | |
| 246 | if (solo_dev->i2c_state == IIC_STATE_STOP) |
| 247 | break; |
| 248 | |
| 249 | timeout = schedule_timeout(timeout); |
| 250 | if (!timeout) |
| 251 | break; |
| 252 | |
| 253 | if (signal_pending(current)) |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | finish_wait(&solo_dev->i2c_wait, &wait); |
| 258 | ret = num - solo_dev->i2c_msg_num; |
| 259 | solo_dev->i2c_state = IIC_STATE_IDLE; |
| 260 | solo_dev->i2c_id = -1; |
| 261 | |
| 262 | mutex_unlock(&solo_dev->i2c_mutex); |
| 263 | |
| 264 | return ret; |
| 265 | } |
| 266 | |
| 267 | static u32 solo_i2c_functionality(struct i2c_adapter *adap) |
| 268 | { |
| 269 | return I2C_FUNC_I2C; |
| 270 | } |
| 271 | |
| 272 | static const struct i2c_algorithm solo_i2c_algo = { |
| 273 | .master_xfer = solo_i2c_master_xfer, |
| 274 | .functionality = solo_i2c_functionality, |
| 275 | }; |
| 276 | |
| 277 | int solo_i2c_init(struct solo_dev *solo_dev) |
| 278 | { |
| 279 | int i; |
| 280 | int ret; |
| 281 | |
| 282 | solo_reg_write(solo_dev, SOLO_IIC_CFG, |
| 283 | SOLO_IIC_PRESCALE(8) | SOLO_IIC_ENABLE); |
| 284 | |
| 285 | solo_dev->i2c_id = -1; |
| 286 | solo_dev->i2c_state = IIC_STATE_IDLE; |
| 287 | init_waitqueue_head(&solo_dev->i2c_wait); |
| 288 | mutex_init(&solo_dev->i2c_mutex); |
| 289 | |
| 290 | for (i = 0; i < SOLO_I2C_ADAPTERS; i++) { |
| 291 | struct i2c_adapter *adap = &solo_dev->i2c_adap[i]; |
| 292 | |
| 293 | snprintf(adap->name, I2C_NAME_SIZE, "%s I2C %d", |
| 294 | SOLO6X10_NAME, i); |
| 295 | adap->algo = &solo_i2c_algo; |
| 296 | adap->algo_data = solo_dev; |
| 297 | adap->retries = 1; |
| 298 | adap->dev.parent = &solo_dev->pdev->dev; |
| 299 | |
| 300 | ret = i2c_add_adapter(adap); |
| 301 | if (ret) { |
| 302 | adap->algo_data = NULL; |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | if (ret) { |
| 308 | for (i = 0; i < SOLO_I2C_ADAPTERS; i++) { |
| 309 | if (!solo_dev->i2c_adap[i].algo_data) |
| 310 | break; |
| 311 | i2c_del_adapter(&solo_dev->i2c_adap[i]); |
| 312 | solo_dev->i2c_adap[i].algo_data = NULL; |
| 313 | } |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | void solo_i2c_exit(struct solo_dev *solo_dev) |
| 321 | { |
| 322 | int i; |
| 323 | |
| 324 | for (i = 0; i < SOLO_I2C_ADAPTERS; i++) { |
| 325 | if (!solo_dev->i2c_adap[i].algo_data) |
| 326 | continue; |
| 327 | i2c_del_adapter(&solo_dev->i2c_adap[i]); |
| 328 | solo_dev->i2c_adap[i].algo_data = NULL; |
| 329 | } |
| 330 | } |