Bar Weiner | f82c587 | 2012-10-23 14:31:26 +0200 | [diff] [blame] | 1 | /* Copyright (c) 2012, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | /* This driver implements a simple SPI read/write interface to access |
| 15 | * an external device over SPI. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/spi/spi.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/device.h> |
| 24 | #include <linux/cdev.h> |
| 25 | #include <linux/fs.h> |
| 26 | #include <linux/mutex.h> |
| 27 | #include <linux/uaccess.h> |
| 28 | #include <linux/gpio.h> |
| 29 | #include <linux/delay.h> |
| 30 | |
| 31 | #include <linux/ci-bridge-spi.h> |
| 32 | |
| 33 | #define CI_MAX_BUFFER_SIZE (64 * 1024) |
| 34 | |
| 35 | struct ci_bridge { |
| 36 | dev_t ci_bridge_dev; |
| 37 | struct cdev cdev; |
| 38 | struct class *bridge_class; |
| 39 | struct device *bridge_dev; |
| 40 | char *write_buffer; |
| 41 | char *read_buffer; |
| 42 | struct mutex lock; |
| 43 | struct spi_device *spi; |
| 44 | unsigned int gpio_reset_pin; |
| 45 | unsigned int gpio_interrupt_pin; |
| 46 | int num_opened; |
| 47 | |
| 48 | }; |
| 49 | |
| 50 | static struct ci_bridge ci; |
| 51 | |
| 52 | static int __devinit ci_bridge_spi_probe(struct spi_device *spi) |
| 53 | { |
| 54 | int ret; |
| 55 | struct ci_bridge_platform_data *pdata; |
| 56 | |
| 57 | if (spi->dev.platform_data == NULL) { |
| 58 | pr_err("%s: platform data is missing\n", __func__); |
| 59 | return -EINVAL; |
| 60 | } |
| 61 | |
| 62 | ci.spi = spi; |
| 63 | ci.num_opened = 0; |
| 64 | mutex_init(&ci.lock); |
| 65 | spi_set_drvdata(spi, &ci); |
| 66 | pdata = spi->dev.platform_data; |
| 67 | ci.gpio_reset_pin = pdata->reset_pin; |
| 68 | ci.gpio_interrupt_pin = pdata->interrupt_pin; |
| 69 | |
| 70 | ret = gpio_request(ci.gpio_reset_pin, "ci_bridge_spi"); |
| 71 | if (ret) { |
| 72 | pr_err("%s: GPIO request for pin number %u failed\n", |
| 73 | __func__, ci.gpio_reset_pin); |
| 74 | return ret; |
| 75 | } |
| 76 | ret = gpio_direction_output(ci.gpio_reset_pin, 1); |
| 77 | if (ret) { |
| 78 | pr_err("%s: unable to set GPIO direction, err=%d\n", |
| 79 | __func__, ret); |
| 80 | goto err_free_reset_pin; |
| 81 | } |
| 82 | |
| 83 | ret = gpio_request(ci.gpio_interrupt_pin, "ci_bridge_spi"); |
| 84 | if (ret) { |
| 85 | pr_err("%s: GPIO request for pin number %u failed\n", |
| 86 | __func__, ci.gpio_interrupt_pin); |
| 87 | goto err_free_reset_pin; |
| 88 | } |
| 89 | ret = gpio_direction_input(ci.gpio_interrupt_pin); |
| 90 | if (ret) { |
| 91 | pr_err("%s: unable to set GPIO direction, err=%d\n", |
| 92 | __func__, ret); |
| 93 | goto err_free_int_pin; |
| 94 | } |
| 95 | |
| 96 | return 0; |
| 97 | |
| 98 | err_free_int_pin: |
| 99 | gpio_free(ci.gpio_interrupt_pin); |
| 100 | err_free_reset_pin: |
| 101 | gpio_free(ci.gpio_reset_pin); |
| 102 | |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | static int __devexit ci_bridge_spi_remove(struct spi_device *spi) |
| 107 | { |
| 108 | struct ci_bridge *bridge = spi_get_drvdata(spi); |
| 109 | |
| 110 | spi_set_drvdata(bridge->spi, NULL); |
| 111 | bridge->spi = NULL; |
| 112 | mutex_destroy(&ci.lock); |
| 113 | |
| 114 | gpio_free(ci.gpio_reset_pin); |
| 115 | gpio_free(ci.gpio_interrupt_pin); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static struct spi_driver ci_bridge_driver = { |
| 121 | .driver = { |
| 122 | .name = "ci_bridge_spi", |
| 123 | .owner = THIS_MODULE, |
| 124 | }, |
| 125 | .probe = ci_bridge_spi_probe, |
| 126 | .remove = __devexit_p(ci_bridge_spi_remove), |
| 127 | }; |
| 128 | |
| 129 | static void ci_bridge_spi_completion_cb(void *arg) |
| 130 | { |
| 131 | complete(arg); |
| 132 | } |
| 133 | |
| 134 | static ssize_t ci_bridge_spi_read(struct file *filp, |
| 135 | char __user *buf, |
| 136 | size_t count, |
| 137 | loff_t *f_pos) |
| 138 | { |
| 139 | int ret = 0; |
| 140 | unsigned long not_copied = 0; |
| 141 | struct spi_transfer spi_transfer; |
| 142 | struct spi_message spi_message; |
| 143 | DECLARE_COMPLETION_ONSTACK(context); |
| 144 | struct ci_bridge *bridge = filp->private_data; |
| 145 | |
| 146 | if ((bridge == NULL) || (bridge->spi == NULL)) |
| 147 | return -ENODEV; |
| 148 | |
| 149 | if (count > CI_MAX_BUFFER_SIZE) |
| 150 | return -EMSGSIZE; |
| 151 | |
| 152 | memset(&spi_transfer, 0, sizeof(struct spi_transfer)); |
| 153 | memset(&spi_message, 0, sizeof(struct spi_message)); |
| 154 | |
| 155 | mutex_lock(&bridge->lock); |
| 156 | |
| 157 | spi_transfer.rx_buf = bridge->read_buffer; |
| 158 | spi_transfer.len = count; |
| 159 | spi_message_init(&spi_message); |
| 160 | spi_message_add_tail(&spi_transfer, &spi_message); |
| 161 | spi_message.complete = ci_bridge_spi_completion_cb; |
| 162 | spi_message.context = &context; |
| 163 | |
| 164 | /* must use spi_async in a context that may sleep */ |
| 165 | ret = spi_async(bridge->spi, &spi_message); |
| 166 | if (ret == 0) { |
| 167 | wait_for_completion(&context); |
| 168 | |
| 169 | if (spi_message.status == 0) { |
| 170 | /* spi_message.actual_length should contain the number |
| 171 | * of bytes actually read and should update ret to be |
| 172 | * the actual length, but since our driver doesn't |
| 173 | * support this, assume all count bytes were read. |
| 174 | */ |
| 175 | ret = count; |
| 176 | } |
| 177 | |
| 178 | if (ret > 0) { |
| 179 | not_copied = |
| 180 | copy_to_user(buf, bridge->read_buffer, ret); |
| 181 | if (not_copied == ret) |
| 182 | ret = -EFAULT; |
| 183 | else |
| 184 | ret -= not_copied; |
| 185 | } |
| 186 | } else { |
| 187 | pr_err("%s: Error calling spi_async, ret = %d\n", |
| 188 | __func__, ret); |
| 189 | } |
| 190 | |
| 191 | mutex_unlock(&bridge->lock); |
| 192 | |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | static ssize_t ci_bridge_spi_write(struct file *filp, |
| 197 | const char __user *buf, |
| 198 | size_t count, |
| 199 | loff_t *f_pos) |
| 200 | { |
| 201 | int ret = 0; |
| 202 | unsigned long not_copied = 0; |
| 203 | struct spi_transfer spi_transfer; |
| 204 | struct spi_message spi_message; |
| 205 | DECLARE_COMPLETION_ONSTACK(context); |
| 206 | struct ci_bridge *bridge = filp->private_data; |
| 207 | |
| 208 | if ((bridge == NULL) || (bridge->spi == NULL)) |
| 209 | return -ENODEV; |
| 210 | |
| 211 | if (count > CI_MAX_BUFFER_SIZE) |
| 212 | return -EMSGSIZE; |
| 213 | |
| 214 | memset(&spi_transfer, 0, sizeof(struct spi_transfer)); |
| 215 | memset(&spi_message, 0, sizeof(struct spi_message)); |
| 216 | |
| 217 | mutex_lock(&bridge->lock); |
| 218 | /* copy user data to our SPI Tx buffer */ |
| 219 | not_copied = copy_from_user(bridge->write_buffer, buf, count); |
| 220 | if (not_copied != 0) { |
| 221 | ret = -EFAULT; |
| 222 | } else { |
| 223 | spi_transfer.tx_buf = bridge->write_buffer; |
| 224 | spi_transfer.len = count; |
| 225 | |
| 226 | spi_message_init(&spi_message); |
| 227 | spi_message_add_tail(&spi_transfer, &spi_message); |
| 228 | spi_message.complete = ci_bridge_spi_completion_cb; |
| 229 | spi_message.context = &context; |
| 230 | |
| 231 | /* must use spi_async in a context that may sleep */ |
| 232 | ret = spi_async(bridge->spi, &spi_message); |
| 233 | if (ret == 0) { |
| 234 | wait_for_completion(&context); |
| 235 | /* update ret to contain |
| 236 | * the number of bytes actually written |
| 237 | */ |
| 238 | if (spi_message.status == 0) |
| 239 | ret = spi_transfer.len; |
| 240 | else |
| 241 | pr_err("%s: SPI transfer error, spi_message.status = %d\n", |
| 242 | __func__, spi_message.status); |
| 243 | } else { |
| 244 | pr_err("%s: Error calling spi_async, ret = %d\n", |
| 245 | __func__, ret); |
| 246 | } |
| 247 | } |
| 248 | mutex_unlock(&bridge->lock); |
| 249 | |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | static int ci_bridge_spi_open(struct inode *inode, struct file *filp) |
| 254 | { |
| 255 | /* forbid opening more then one instance at a time, |
| 256 | parallel execution can still be problematic */ |
| 257 | if (ci.num_opened != 0) |
| 258 | return -EBUSY; |
| 259 | |
| 260 | /* allocate write buffer */ |
| 261 | ci.write_buffer = |
| 262 | kzalloc((CI_MAX_BUFFER_SIZE * sizeof(char)), GFP_KERNEL); |
| 263 | if (ci.write_buffer == NULL) { |
| 264 | pr_err("%s: Error allocating memory for write buffer\n", |
| 265 | __func__); |
| 266 | return -ENOMEM; |
| 267 | } |
| 268 | /* allocate read buffer */ |
| 269 | ci.read_buffer = |
| 270 | kzalloc((CI_MAX_BUFFER_SIZE * sizeof(char)), GFP_KERNEL); |
| 271 | if (ci.read_buffer == NULL) { |
| 272 | pr_err("%s: Error allocating memory for read buffer\n", |
| 273 | __func__); |
| 274 | kfree(ci.write_buffer); |
| 275 | return -ENOMEM; |
| 276 | } |
| 277 | /* device is non-seekable */ |
| 278 | nonseekable_open(inode, filp); |
| 279 | |
| 280 | filp->private_data = &ci; |
| 281 | ci.num_opened = 1; |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static int ci_bridge_ioctl_get_int(void *arg) |
| 287 | { |
| 288 | int state; |
| 289 | |
| 290 | if (arg == NULL) |
| 291 | return -EINVAL; |
| 292 | |
| 293 | state = gpio_get_value_cansleep(ci.gpio_interrupt_pin); |
| 294 | if (copy_to_user(arg, &state, sizeof(state))) |
| 295 | return -EFAULT; |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static int ci_bridge_ioctl_reset(unsigned long arg) |
| 301 | { |
| 302 | if ((arg != 0) && (arg != 1)) |
| 303 | return -EINVAL; |
| 304 | |
| 305 | gpio_set_value_cansleep(ci.gpio_reset_pin, arg); |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static long ci_bridge_spi_ioctl(struct file *file, unsigned int cmd, |
| 311 | unsigned long arg) |
| 312 | { |
| 313 | int ret; |
| 314 | |
| 315 | switch (cmd) { |
| 316 | |
| 317 | case CI_BRIDGE_IOCTL_RESET: |
| 318 | ret = ci_bridge_ioctl_reset(arg); |
| 319 | break; |
| 320 | |
| 321 | case CI_BRIDGE_IOCTL_GET_INT_STATE: |
| 322 | ret = ci_bridge_ioctl_get_int((void *) arg); |
| 323 | break; |
| 324 | |
| 325 | default: |
| 326 | ret = -EINVAL; |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | return ret; |
| 331 | } |
| 332 | |
| 333 | static int ci_bridge_spi_release(struct inode *inode, struct file *filp) |
| 334 | { |
| 335 | struct ci_bridge *bridge = filp->private_data; |
| 336 | |
| 337 | if ((bridge == NULL) || (bridge->spi == NULL)) |
| 338 | return -ENODEV; |
| 339 | |
| 340 | kfree(bridge->write_buffer); |
| 341 | kfree(bridge->read_buffer); |
| 342 | filp->private_data = NULL; |
| 343 | ci.num_opened = 0; |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static const struct file_operations ci_bridge_spi_fops = { |
| 349 | .owner = THIS_MODULE, |
| 350 | .read = ci_bridge_spi_read, |
| 351 | .write = ci_bridge_spi_write, |
| 352 | .open = ci_bridge_spi_open, |
| 353 | .unlocked_ioctl = ci_bridge_spi_ioctl, |
| 354 | .release = ci_bridge_spi_release, |
| 355 | .llseek = no_llseek, |
| 356 | }; |
| 357 | |
| 358 | static int __init ci_bridge_init(void) |
| 359 | { |
| 360 | int ret = 0; |
| 361 | |
| 362 | ret = alloc_chrdev_region(&ci.ci_bridge_dev, 0, 1, "ci_bridge_spi"); |
| 363 | if (ret != 0) |
| 364 | return ret; |
| 365 | |
| 366 | ci.bridge_class = class_create(THIS_MODULE, "ci_bridge_spi"); |
| 367 | if (IS_ERR(ci.bridge_class)) { |
| 368 | ret = PTR_ERR(ci.bridge_class); |
| 369 | pr_err("Error creating ci.bridge_class: %d\n", ret); |
| 370 | goto free_region; |
| 371 | } |
| 372 | |
| 373 | cdev_init(&ci.cdev, &ci_bridge_spi_fops); |
| 374 | ci.cdev.owner = THIS_MODULE; |
| 375 | ret = cdev_add(&ci.cdev, ci.ci_bridge_dev, 1); |
| 376 | if (ret != 0) { |
| 377 | pr_err("Error calling cdev_add: %d\n", ret); |
| 378 | goto class_destroy; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | ci.bridge_dev = device_create(ci.bridge_class, NULL, ci.cdev.dev, |
| 383 | &ci, "ci_bridge_spi0"); |
| 384 | if (IS_ERR(ci.bridge_dev)) { |
| 385 | ret = PTR_ERR(ci.bridge_dev); |
| 386 | pr_err("device_create failed: %d\n", ret); |
| 387 | goto del_cdev; |
| 388 | } |
| 389 | |
| 390 | ret = spi_register_driver(&ci_bridge_driver); |
| 391 | if (ret != 0) { |
| 392 | pr_err("Error registering spi driver: %d\n", ret); |
| 393 | goto device_destroy; |
| 394 | } |
| 395 | |
| 396 | /* successful return */ |
| 397 | return 0; |
| 398 | |
| 399 | device_destroy: |
| 400 | device_destroy(ci.bridge_class, ci.ci_bridge_dev); |
| 401 | |
| 402 | del_cdev: |
| 403 | cdev_del(&ci.cdev); |
| 404 | |
| 405 | class_destroy: |
| 406 | class_destroy(ci.bridge_class); |
| 407 | |
| 408 | free_region: |
| 409 | unregister_chrdev_region(ci.ci_bridge_dev, 1); |
| 410 | |
| 411 | return ret; |
| 412 | } |
| 413 | |
| 414 | static void __exit ci_bridge_exit(void) |
| 415 | { |
| 416 | spi_unregister_driver(&ci_bridge_driver); |
| 417 | device_destroy(ci.bridge_class, ci.ci_bridge_dev); |
| 418 | cdev_del(&ci.cdev); |
| 419 | class_destroy(ci.bridge_class); |
| 420 | unregister_chrdev_region(ci.ci_bridge_dev, 1); |
| 421 | } |
| 422 | |
| 423 | module_init(ci_bridge_init); |
| 424 | module_exit(ci_bridge_exit); |
| 425 | |
| 426 | MODULE_DESCRIPTION("CI Bridge SPI Driver"); |
| 427 | MODULE_LICENSE("GPL v2"); |
| 428 | |