| 1 | --- /dev/null |
| 2 | +++ b/drivers/cbus/cbus.c |
| 3 | @@ -0,0 +1,335 @@ |
| 4 | +/* |
| 5 | + * drivers/cbus/cbus.c |
| 6 | + * |
| 7 | + * Support functions for CBUS serial protocol |
| 8 | + * |
| 9 | + * Copyright (C) 2004-2010 Nokia Corporation |
| 10 | + * Contact: Felipe Balbi <felipe.balbi@nokia.com> |
| 11 | + * |
| 12 | + * Written by Juha Yrjölä <juha.yrjola@nokia.com>, |
| 13 | + * David Weinehall <david.weinehall@nokia.com>, and |
| 14 | + * Mikko Ylinen <mikko.k.ylinen@nokia.com> |
| 15 | + * |
| 16 | + * Several updates and cleanups by Felipe Balbi <felipe.balbi@nokia.com> |
| 17 | + * |
| 18 | + * This file is subject to the terms and conditions of the GNU General |
| 19 | + * Public License. See the file "COPYING" in the main directory of this |
| 20 | + * archive for more details. |
| 21 | + * |
| 22 | + * This program is distributed in the hope that it will be useful, |
| 23 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | + * GNU General Public License for more details. |
| 26 | + * |
| 27 | + * You should have received a copy of the GNU General Public License |
| 28 | + * along with this program; if not, write to the Free Software |
| 29 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 30 | + */ |
| 31 | + |
| 32 | +#include <linux/device.h> |
| 33 | +#include <linux/init.h> |
| 34 | +#include <linux/kernel.h> |
| 35 | +#include <linux/export.h> |
| 36 | +#include <linux/module.h> |
| 37 | +#include <linux/slab.h> |
| 38 | +#include <linux/spinlock.h> |
| 39 | +#include <linux/gpio.h> |
| 40 | +#include <linux/platform_device.h> |
| 41 | +#include <linux/platform_data/cbus.h> |
| 42 | + |
| 43 | +#include "cbus.h" |
| 44 | + |
| 45 | +#define CBUS_XFER_READ 1 |
| 46 | +#define CBUS_XFER_WRITE 0 |
| 47 | + |
| 48 | +struct cbus_host { |
| 49 | + /* host lock */ |
| 50 | + spinlock_t lock; |
| 51 | + |
| 52 | + struct device *dev; |
| 53 | + |
| 54 | + int clk_gpio; |
| 55 | + int dat_gpio; |
| 56 | + int sel_gpio; |
| 57 | +}; |
| 58 | + |
| 59 | +/** |
| 60 | + * cbus_send_bit - sends one bit over the bus |
| 61 | + * @host: the host we're using |
| 62 | + * @bit: one bit of information to send |
| 63 | + * @input: whether to set data pin as input after sending |
| 64 | + */ |
| 65 | +static int cbus_send_bit(struct cbus_host *host, unsigned bit, |
| 66 | + unsigned input) |
| 67 | +{ |
| 68 | + int ret = 0; |
| 69 | + |
| 70 | + gpio_set_value(host->dat_gpio, bit ? 1 : 0); |
| 71 | + gpio_set_value(host->clk_gpio, 1); |
| 72 | + |
| 73 | + /* The data bit is read on the rising edge of CLK */ |
| 74 | + if (input) |
| 75 | + ret = gpio_direction_input(host->dat_gpio); |
| 76 | + |
| 77 | + gpio_set_value(host->clk_gpio, 0); |
| 78 | + |
| 79 | + return ret; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * cbus_send_data - sends @len amount of data over the bus |
| 84 | + * @host: the host we're using |
| 85 | + * @data: the data to send |
| 86 | + * @len: size of the transfer |
| 87 | + * @input: whether to set data pin as input after sending |
| 88 | + */ |
| 89 | +static int cbus_send_data(struct cbus_host *host, unsigned data, unsigned len, |
| 90 | + unsigned input) |
| 91 | +{ |
| 92 | + int ret = 0; |
| 93 | + int i; |
| 94 | + |
| 95 | + for (i = len; i > 0; i--) { |
| 96 | + ret = cbus_send_bit(host, data & (1 << (i - 1)), |
| 97 | + input && (i == 1)); |
| 98 | + if (ret < 0) |
| 99 | + goto out; |
| 100 | + } |
| 101 | + |
| 102 | +out: |
| 103 | + return ret; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * cbus_receive_bit - receives one bit from the bus |
| 108 | + * @host: the host we're using |
| 109 | + */ |
| 110 | +static int cbus_receive_bit(struct cbus_host *host) |
| 111 | +{ |
| 112 | + int ret; |
| 113 | + |
| 114 | + gpio_set_value(host->clk_gpio, 1); |
| 115 | + ret = gpio_get_value(host->dat_gpio); |
| 116 | + if (ret < 0) |
| 117 | + goto out; |
| 118 | + gpio_set_value(host->clk_gpio, 0); |
| 119 | + |
| 120 | +out: |
| 121 | + return ret; |
| 122 | +} |
| 123 | + |
| 124 | +/** |
| 125 | + * cbus_receive_data - receives @len data from the bus |
| 126 | + * @host: the host we're using |
| 127 | + * @len: the length of data to receive |
| 128 | + */ |
| 129 | +static int cbus_receive_data(struct cbus_host *host, unsigned len) |
| 130 | +{ |
| 131 | + int ret = 0; |
| 132 | + int i; |
| 133 | + |
| 134 | + for (i = 16; i > 0; i--) { |
| 135 | + int bit = cbus_receive_bit(host); |
| 136 | + |
| 137 | + if (bit < 0) |
| 138 | + goto out; |
| 139 | + |
| 140 | + if (bit) |
| 141 | + ret |= 1 << (i - 1); |
| 142 | + } |
| 143 | + |
| 144 | +out: |
| 145 | + return ret; |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * cbus_transfer - transfers data over the bus |
| 150 | + * @host: the host we're using |
| 151 | + * @rw: read/write flag |
| 152 | + * @dev: device address |
| 153 | + * @reg: register address |
| 154 | + * @data: if @rw == 0 data to send otherwise 0 |
| 155 | + */ |
| 156 | +static int cbus_transfer(struct cbus_host *host, unsigned rw, unsigned dev, |
| 157 | + unsigned reg, unsigned data) |
| 158 | +{ |
| 159 | + unsigned long flags; |
| 160 | + int input = 0; |
| 161 | + int ret = 0; |
| 162 | + |
| 163 | + /* We don't want interrupts disturbing our transfer */ |
| 164 | + spin_lock_irqsave(&host->lock, flags); |
| 165 | + |
| 166 | + /* Reset state and start of transfer, SEL stays down during transfer */ |
| 167 | + gpio_set_value(host->sel_gpio, 0); |
| 168 | + |
| 169 | + /* Set the DAT pin to output */ |
| 170 | + gpio_direction_output(host->dat_gpio, 1); |
| 171 | + |
| 172 | + /* Send the device address */ |
| 173 | + ret = cbus_send_data(host, dev, 3, 0); |
| 174 | + if (ret < 0) { |
| 175 | + dev_dbg(host->dev, "failed sending device addr\n"); |
| 176 | + goto out; |
| 177 | + } |
| 178 | + |
| 179 | + /* Send the rw flag */ |
| 180 | + ret = cbus_send_bit(host, rw, 0); |
| 181 | + if (ret < 0) { |
| 182 | + dev_dbg(host->dev, "failed sending read/write flag\n"); |
| 183 | + goto out; |
| 184 | + } |
| 185 | + |
| 186 | + /* Send the register address */ |
| 187 | + if (rw) |
| 188 | + input = true; |
| 189 | + |
| 190 | + ret = cbus_send_data(host, reg, 5, input); |
| 191 | + if (ret < 0) { |
| 192 | + dev_dbg(host->dev, "failed sending register addr\n"); |
| 193 | + goto out; |
| 194 | + } |
| 195 | + |
| 196 | + if (!rw) { |
| 197 | + ret = cbus_send_data(host, data, 16, 0); |
| 198 | + if (ret < 0) { |
| 199 | + dev_dbg(host->dev, "failed sending data\n"); |
| 200 | + goto out; |
| 201 | + } |
| 202 | + } else { |
| 203 | + gpio_set_value(host->clk_gpio, 1); |
| 204 | + |
| 205 | + ret = cbus_receive_data(host, 16); |
| 206 | + if (ret < 0) { |
| 207 | + dev_dbg(host->dev, "failed receiving data\n"); |
| 208 | + goto out; |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + /* Indicate end of transfer, SEL goes up until next transfer */ |
| 213 | + gpio_set_value(host->sel_gpio, 1); |
| 214 | + gpio_set_value(host->clk_gpio, 1); |
| 215 | + gpio_set_value(host->clk_gpio, 0); |
| 216 | + |
| 217 | +out: |
| 218 | + spin_unlock_irqrestore(&host->lock, flags); |
| 219 | + |
| 220 | + return ret; |
| 221 | +} |
| 222 | + |
| 223 | +/** |
| 224 | + * cbus_read_reg - reads a given register from the device |
| 225 | + * @child: the child device |
| 226 | + * @dev: device address |
| 227 | + * @reg: register address |
| 228 | + */ |
| 229 | +int cbus_read_reg(struct device *child, unsigned dev, unsigned reg) |
| 230 | +{ |
| 231 | + struct cbus_host *host = dev_get_drvdata(child->parent); |
| 232 | + |
| 233 | + return cbus_transfer(host, CBUS_XFER_READ, dev, reg, 0); |
| 234 | +} |
| 235 | +EXPORT_SYMBOL(cbus_read_reg); |
| 236 | + |
| 237 | +/** |
| 238 | + * cbus_write_reg - writes to a given register of the device |
| 239 | + * @child: the child device |
| 240 | + * @dev: device address |
| 241 | + * @reg: register address |
| 242 | + * @val: data to be written to @reg |
| 243 | + */ |
| 244 | +int cbus_write_reg(struct device *child, unsigned dev, unsigned reg, |
| 245 | + unsigned val) |
| 246 | +{ |
| 247 | + struct cbus_host *host = dev_get_drvdata(child->parent); |
| 248 | + |
| 249 | + return cbus_transfer(host, CBUS_XFER_WRITE, dev, reg, val); |
| 250 | +} |
| 251 | +EXPORT_SYMBOL(cbus_write_reg); |
| 252 | + |
| 253 | +static int __init cbus_bus_probe(struct platform_device *pdev) |
| 254 | +{ |
| 255 | + struct cbus_host *chost; |
| 256 | + struct cbus_host_platform_data *pdata = pdev->dev.platform_data; |
| 257 | + int ret; |
| 258 | + |
| 259 | + chost = kzalloc(sizeof(*chost), GFP_KERNEL); |
| 260 | + if (chost == NULL) |
| 261 | + return -ENOMEM; |
| 262 | + |
| 263 | + spin_lock_init(&chost->lock); |
| 264 | + |
| 265 | + chost->clk_gpio = pdata->clk_gpio; |
| 266 | + chost->dat_gpio = pdata->dat_gpio; |
| 267 | + chost->sel_gpio = pdata->sel_gpio; |
| 268 | + chost->dev = &pdev->dev; |
| 269 | + |
| 270 | + ret = gpio_request(chost->clk_gpio, "CBUS clk"); |
| 271 | + if (ret < 0) |
| 272 | + goto exit1; |
| 273 | + |
| 274 | + ret = gpio_request(chost->dat_gpio, "CBUS data"); |
| 275 | + if (ret < 0) |
| 276 | + goto exit2; |
| 277 | + |
| 278 | + ret = gpio_request(chost->sel_gpio, "CBUS sel"); |
| 279 | + if (ret < 0) |
| 280 | + goto exit3; |
| 281 | + |
| 282 | + gpio_direction_output(chost->clk_gpio, 0); |
| 283 | + gpio_direction_input(chost->dat_gpio); |
| 284 | + gpio_direction_output(chost->sel_gpio, 1); |
| 285 | + |
| 286 | + gpio_set_value(chost->clk_gpio, 1); |
| 287 | + gpio_set_value(chost->clk_gpio, 0); |
| 288 | + |
| 289 | + platform_set_drvdata(pdev, chost); |
| 290 | + |
| 291 | + return 0; |
| 292 | +exit3: |
| 293 | + gpio_free(chost->dat_gpio); |
| 294 | +exit2: |
| 295 | + gpio_free(chost->clk_gpio); |
| 296 | +exit1: |
| 297 | + kfree(chost); |
| 298 | + |
| 299 | + return ret; |
| 300 | +} |
| 301 | + |
| 302 | +static void __exit cbus_bus_remove(struct platform_device *pdev) |
| 303 | +{ |
| 304 | + struct cbus_host *chost = platform_get_drvdata(pdev); |
| 305 | + |
| 306 | + gpio_free(chost->sel_gpio); |
| 307 | + gpio_free(chost->dat_gpio); |
| 308 | + gpio_free(chost->clk_gpio); |
| 309 | + |
| 310 | + kfree(chost); |
| 311 | +} |
| 312 | + |
| 313 | +static struct platform_driver cbus_driver = { |
| 314 | + .remove = __exit_p(cbus_bus_remove), |
| 315 | + .driver = { |
| 316 | + .name = "cbus", |
| 317 | + }, |
| 318 | +}; |
| 319 | + |
| 320 | +static int __init cbus_bus_init(void) |
| 321 | +{ |
| 322 | + return platform_driver_probe(&cbus_driver, cbus_bus_probe); |
| 323 | +} |
| 324 | +subsys_initcall(cbus_bus_init); |
| 325 | + |
| 326 | +static void __exit cbus_bus_exit(void) |
| 327 | +{ |
| 328 | + platform_driver_unregister(&cbus_driver); |
| 329 | +} |
| 330 | +module_exit(cbus_bus_exit); |
| 331 | + |
| 332 | +MODULE_DESCRIPTION("CBUS serial protocol"); |
| 333 | +MODULE_LICENSE("GPL"); |
| 334 | +MODULE_AUTHOR("Juha Yrjölä"); |
| 335 | +MODULE_AUTHOR("David Weinehall"); |
| 336 | +MODULE_AUTHOR("Mikko Ylinen"); |
| 337 | +MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>"); |
| 338 | + |
| 339 | --- /dev/null |
| 340 | +++ b/drivers/cbus/cbus.h |
| 341 | @@ -0,0 +1,30 @@ |
| 342 | +/* |
| 343 | + * drivers/cbus/cbus.h |
| 344 | + * |
| 345 | + * Copyright (C) 2004, 2005 Nokia Corporation |
| 346 | + * |
| 347 | + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and |
| 348 | + * David Weinehall <david.weinehall@nokia.com> |
| 349 | + * |
| 350 | + * This file is subject to the terms and conditions of the GNU General |
| 351 | + * Public License. See the file "COPYING" in the main directory of this |
| 352 | + * archive for more details. |
| 353 | + * |
| 354 | + * This program is distributed in the hope that it will be useful, |
| 355 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 356 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 357 | + * GNU General Public License for more details. |
| 358 | + * |
| 359 | + * You should have received a copy of the GNU General Public License |
| 360 | + * along with this program; if not, write to the Free Software |
| 361 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 362 | + */ |
| 363 | + |
| 364 | +#ifndef __DRIVERS_CBUS_CBUS_H |
| 365 | +#define __DRIVERS_CBUS_CBUS_H |
| 366 | + |
| 367 | +extern int cbus_read_reg(struct device *, unsigned dev, unsigned reg); |
| 368 | +extern int cbus_write_reg(struct device *, unsigned dev, unsigned reg, |
| 369 | + unsigned val); |
| 370 | + |
| 371 | +#endif /* __DRIVERS_CBUS_CBUS_H */ |
| 372 | --- /dev/null |
| 373 | +++ b/drivers/cbus/Kconfig |
| 374 | @@ -0,0 +1,86 @@ |
| 375 | +# |
| 376 | +# CBUS device configuration |
| 377 | +# |
| 378 | + |
| 379 | +menu "CBUS support" |
| 380 | + |
| 381 | +config CBUS |
| 382 | + bool "CBUS support on OMAP" |
| 383 | + ---help--- |
| 384 | + CBUS is a proprietary serial protocol by Nokia. It is mainly |
| 385 | + used for accessing Energy Management auxiliary chips. |
| 386 | + |
| 387 | + If you want CBUS support, you should say Y here. |
| 388 | + |
| 389 | +config CBUS_TAHVO |
| 390 | + depends on CBUS |
| 391 | + bool "Support for Tahvo" |
| 392 | + ---help--- |
| 393 | + Tahvo is a mixed signal ASIC with some system features |
| 394 | + |
| 395 | + If you want Tahvo support, you should say Y here. |
| 396 | + |
| 397 | +if CBUS_TAHVO |
| 398 | + |
| 399 | +config CBUS_TAHVO_USB |
| 400 | + depends on USB |
| 401 | + depends on ARCH_OMAP |
| 402 | + select USB_OTG_UTILS |
| 403 | + tristate "Support for Tahvo USB transceiver" |
| 404 | + ---help--- |
| 405 | + If you want Tahvo support for USB transceiver, say Y or M here. |
| 406 | + |
| 407 | +config CBUS_TAHVO_USB_HOST_BY_DEFAULT |
| 408 | + depends on CBUS_TAHVO_USB && USB_OTG |
| 409 | + boolean "Device in USB host mode by default" |
| 410 | + ---help--- |
| 411 | + Say Y here, if you want the device to enter USB host mode |
| 412 | + by default on bootup. |
| 413 | + |
| 414 | +endif # CBUS_TAHVO |
| 415 | + |
| 416 | +config CBUS_RETU |
| 417 | + depends on CBUS |
| 418 | + bool "Support for Retu" |
| 419 | + ---help--- |
| 420 | + Retu is a mixed signal ASIC with some system features |
| 421 | + |
| 422 | + If you want Retu support, you should say Y here. |
| 423 | + |
| 424 | +if CBUS_RETU |
| 425 | + |
| 426 | +config CBUS_RETU_POWERBUTTON |
| 427 | + depends on INPUT |
| 428 | + bool "Support for Retu power button" |
| 429 | + ---help--- |
| 430 | + The power button on Nokia 770 is connected to the Retu ASIC. |
| 431 | + |
| 432 | + If you want support for the Retu power button, you should say Y here. |
| 433 | + |
| 434 | +config CBUS_RETU_RTC |
| 435 | + depends on RTC_CLASS |
| 436 | + depends on ARCH_OMAP |
| 437 | + tristate "Support for Retu pseudo-RTC" |
| 438 | + ---help--- |
| 439 | + Say Y here if you want support for the device that alleges to be an |
| 440 | + RTC in Retu. This will expose a sysfs interface for it. |
| 441 | + |
| 442 | +config CBUS_RETU_WDT |
| 443 | + depends on SYSFS && WATCHDOG |
| 444 | + depends on ARCH_OMAP |
| 445 | + tristate "Support for Retu watchdog timer" |
| 446 | + ---help--- |
| 447 | + Say Y here if you want support for the watchdog in Retu. This will |
| 448 | + expose a sysfs interface to grok it. |
| 449 | + |
| 450 | +config CBUS_RETU_HEADSET |
| 451 | + depends on SYSFS |
| 452 | + tristate "Support for headset detection with Retu/Vilma" |
| 453 | + ---help--- |
| 454 | + Say Y here if you want support detecting a headset that's connected |
| 455 | + to Retu/Vilma. Detection state and events are exposed through |
| 456 | + sysfs. |
| 457 | + |
| 458 | +endif # CBUS_RETU |
| 459 | + |
| 460 | +endmenu |
| 461 | --- /dev/null |
| 462 | +++ b/drivers/cbus/Makefile |
| 463 | @@ -0,0 +1,13 @@ |
| 464 | +# |
| 465 | +# Makefile for CBUS. |
| 466 | +# |
| 467 | + |
| 468 | +obj-$(CONFIG_CBUS) += cbus.o |
| 469 | +obj-$(CONFIG_CBUS_TAHVO) += tahvo.o |
| 470 | +obj-$(CONFIG_CBUS_RETU) += retu.o |
| 471 | +obj-$(CONFIG_CBUS_TAHVO_USB) += tahvo-usb.o |
| 472 | + |
| 473 | +obj-$(CONFIG_CBUS_RETU_POWERBUTTON) += retu-pwrbutton.o |
| 474 | +obj-$(CONFIG_CBUS_RETU_RTC) += retu-rtc.o |
| 475 | +obj-$(CONFIG_CBUS_RETU_WDT) += retu-wdt.o |
| 476 | +obj-$(CONFIG_CBUS_RETU_HEADSET) += retu-headset.o |
| 477 | --- /dev/null |
| 478 | +++ b/drivers/cbus/retu.c |
| 479 | @@ -0,0 +1,549 @@ |
| 480 | +/** |
| 481 | + * drivers/cbus/retu.c |
| 482 | + * |
| 483 | + * Support functions for Retu ASIC |
| 484 | + * |
| 485 | + * Copyright (C) 2004, 2005 Nokia Corporation |
| 486 | + * |
| 487 | + * Written by Juha Yrjölä <juha.yrjola@nokia.com>, |
| 488 | + * David Weinehall <david.weinehall@nokia.com>, and |
| 489 | + * Mikko Ylinen <mikko.k.ylinen@nokia.com> |
| 490 | + * |
| 491 | + * This file is subject to the terms and conditions of the GNU General |
| 492 | + * Public License. See the file "COPYING" in the main directory of this |
| 493 | + * archive for more details. |
| 494 | + * |
| 495 | + * This program is distributed in the hope that it will be useful, |
| 496 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 497 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 498 | + * GNU General Public License for more details. |
| 499 | + * |
| 500 | + * You should have received a copy of the GNU General Public License |
| 501 | + * along with this program; if not, write to the Free Software |
| 502 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 503 | + */ |
| 504 | + |
| 505 | +#include <linux/module.h> |
| 506 | +#include <linux/init.h> |
| 507 | + |
| 508 | +#include <linux/slab.h> |
| 509 | +#include <linux/kernel.h> |
| 510 | +#include <linux/errno.h> |
| 511 | +#include <linux/device.h> |
| 512 | +#include <linux/mutex.h> |
| 513 | +#include <linux/irq.h> |
| 514 | +#include <linux/interrupt.h> |
| 515 | +#include <linux/platform_device.h> |
| 516 | +#include <linux/platform_data/cbus.h> |
| 517 | + |
| 518 | +#include <asm/bitops.h> |
| 519 | + |
| 520 | +#include "cbus.h" |
| 521 | +#include "retu.h" |
| 522 | + |
| 523 | +struct retu { |
| 524 | + /* Device lock */ |
| 525 | + struct mutex mutex; |
| 526 | + struct device *dev; |
| 527 | + |
| 528 | + int devid; |
| 529 | + |
| 530 | + int irq_base; |
| 531 | + int irq_end; |
| 532 | + |
| 533 | + int irq; |
| 534 | + |
| 535 | + int ack; |
| 536 | + bool ack_pending; |
| 537 | + |
| 538 | + int mask; |
| 539 | + bool mask_pending; |
| 540 | + |
| 541 | + bool is_vilma; |
| 542 | +}; |
| 543 | + |
| 544 | +static struct retu *the_retu; |
| 545 | + |
| 546 | +/** |
| 547 | + * __retu_read_reg - Read a value from a register in Retu |
| 548 | + * @retu: pointer to retu structure |
| 549 | + * @reg: the register address to read from |
| 550 | + */ |
| 551 | +static int __retu_read_reg(struct retu *retu, unsigned reg) |
| 552 | +{ |
| 553 | + return cbus_read_reg(retu->dev, retu->devid, reg); |
| 554 | +} |
| 555 | + |
| 556 | +/** |
| 557 | + * __retu_write_reg - Writes a value to a register in Retu |
| 558 | + * @retu: pointer to retu structure |
| 559 | + * @reg: the register address to write to |
| 560 | + * @val: the value to write to the register |
| 561 | + */ |
| 562 | +static void __retu_write_reg(struct retu *retu, unsigned reg, u16 val) |
| 563 | +{ |
| 564 | + cbus_write_reg(retu->dev, retu->devid, reg, val); |
| 565 | +} |
| 566 | + |
| 567 | +/** |
| 568 | + * retu_read_reg - Read a value from a register in Retu |
| 569 | + * @child: device pointer for the calling child |
| 570 | + * @reg: the register to read from |
| 571 | + * |
| 572 | + * This function returns the contents of the specified register |
| 573 | + */ |
| 574 | +int retu_read_reg(struct device *child, unsigned reg) |
| 575 | +{ |
| 576 | + struct retu *retu = dev_get_drvdata(child->parent); |
| 577 | + |
| 578 | + return __retu_read_reg(retu, reg); |
| 579 | +} |
| 580 | +EXPORT_SYMBOL_GPL(retu_read_reg); |
| 581 | + |
| 582 | +/** |
| 583 | + * retu_write_reg - Write a value to a register in Retu |
| 584 | + * @child: the pointer to our calling child |
| 585 | + * @reg: the register to write to |
| 586 | + * @val: the value to write to the register |
| 587 | + * |
| 588 | + * This function writes a value to the specified register |
| 589 | + */ |
| 590 | +void retu_write_reg(struct device *child, unsigned reg, u16 val) |
| 591 | +{ |
| 592 | + struct retu *retu = dev_get_drvdata(child->parent); |
| 593 | + |
| 594 | + mutex_lock(&retu->mutex); |
| 595 | + __retu_write_reg(retu, reg, val); |
| 596 | + mutex_unlock(&retu->mutex); |
| 597 | +} |
| 598 | +EXPORT_SYMBOL_GPL(retu_write_reg); |
| 599 | + |
| 600 | +/** |
| 601 | + * retu_set_clear_reg_bits - helper function to read/set/clear bits |
| 602 | + * @child: device pointer to calling child |
| 603 | + * @reg: the register address |
| 604 | + * @set: mask for setting bits |
| 605 | + * @clear: mask for clearing bits |
| 606 | + */ |
| 607 | +void retu_set_clear_reg_bits(struct device *child, unsigned reg, u16 set, |
| 608 | + u16 clear) |
| 609 | +{ |
| 610 | + struct retu *retu = dev_get_drvdata(child->parent); |
| 611 | + u16 w; |
| 612 | + |
| 613 | + mutex_lock(&retu->mutex); |
| 614 | + w = __retu_read_reg(retu, reg); |
| 615 | + w &= ~clear; |
| 616 | + w |= set; |
| 617 | + __retu_write_reg(retu, reg, w); |
| 618 | + mutex_unlock(&retu->mutex); |
| 619 | +} |
| 620 | +EXPORT_SYMBOL_GPL(retu_set_clear_reg_bits); |
| 621 | + |
| 622 | +#define ADC_MAX_CHAN_NUMBER 13 |
| 623 | + |
| 624 | +/** |
| 625 | + * retu_read_adc - Reads AD conversion result |
| 626 | + * @child: device pointer to calling child |
| 627 | + * @channel: the ADC channel to read from |
| 628 | + */ |
| 629 | +int retu_read_adc(struct device *child, int channel) |
| 630 | +{ |
| 631 | + struct retu *retu = dev_get_drvdata(child->parent); |
| 632 | + int res; |
| 633 | + |
| 634 | + if (!retu) |
| 635 | + return -ENODEV; |
| 636 | + |
| 637 | + if (channel < 0 || channel > ADC_MAX_CHAN_NUMBER) |
| 638 | + return -EINVAL; |
| 639 | + |
| 640 | + mutex_lock(&retu->mutex); |
| 641 | + |
| 642 | + if ((channel == 8) && retu->is_vilma) { |
| 643 | + int scr = __retu_read_reg(retu, RETU_REG_ADCSCR); |
| 644 | + int ch = (__retu_read_reg(retu, RETU_REG_ADCR) >> 10) & 0xf; |
| 645 | + if (((scr & 0xff) != 0) && (ch != 8)) |
| 646 | + __retu_write_reg(retu, RETU_REG_ADCSCR, (scr & ~0xff)); |
| 647 | + } |
| 648 | + |
| 649 | + /* Select the channel and read result */ |
| 650 | + __retu_write_reg(retu, RETU_REG_ADCR, channel << 10); |
| 651 | + res = __retu_read_reg(retu, RETU_REG_ADCR) & 0x3ff; |
| 652 | + |
| 653 | + if (retu->is_vilma) |
| 654 | + __retu_write_reg(retu, RETU_REG_ADCR, (1 << 13)); |
| 655 | + |
| 656 | + /* Unlock retu */ |
| 657 | + mutex_unlock(&retu->mutex); |
| 658 | + |
| 659 | + return res; |
| 660 | +} |
| 661 | +EXPORT_SYMBOL_GPL(retu_read_adc); |
| 662 | + |
| 663 | +static irqreturn_t retu_irq_handler(int irq, void *_retu) |
| 664 | +{ |
| 665 | + struct retu *retu = _retu; |
| 666 | + |
| 667 | + u16 idr; |
| 668 | + u16 imr; |
| 669 | + |
| 670 | + mutex_lock(&retu->mutex); |
| 671 | + idr = __retu_read_reg(retu, RETU_REG_IDR); |
| 672 | + imr = __retu_read_reg(retu, RETU_REG_IMR); |
| 673 | + mutex_unlock(&retu->mutex); |
| 674 | + |
| 675 | + idr &= ~imr; |
| 676 | + if (!idr) { |
| 677 | + dev_vdbg(retu->dev, "No IRQ, spurious?\n"); |
| 678 | + return IRQ_NONE; |
| 679 | + } |
| 680 | + |
| 681 | + while (idr) { |
| 682 | + unsigned long pending = __ffs(idr); |
| 683 | + unsigned int irq; |
| 684 | + |
| 685 | + idr &= ~BIT(pending); |
| 686 | + irq = pending + retu->irq_base; |
| 687 | + handle_nested_irq(irq); |
| 688 | + } |
| 689 | + |
| 690 | + return IRQ_HANDLED; |
| 691 | +} |
| 692 | + |
| 693 | +/* -------------------------------------------------------------------------- */ |
| 694 | + |
| 695 | +static void retu_irq_mask(struct irq_data *data) |
| 696 | +{ |
| 697 | + struct retu *retu = irq_data_get_irq_chip_data(data); |
| 698 | + int irq = data->irq; |
| 699 | + |
| 700 | + retu->mask |= (1 << (irq - retu->irq_base)); |
| 701 | + retu->mask_pending = true; |
| 702 | +} |
| 703 | + |
| 704 | +static void retu_irq_unmask(struct irq_data *data) |
| 705 | +{ |
| 706 | + struct retu *retu = irq_data_get_irq_chip_data(data); |
| 707 | + int irq = data->irq; |
| 708 | + |
| 709 | + retu->mask &= ~(1 << (irq - retu->irq_base)); |
| 710 | + retu->mask_pending = true; |
| 711 | + |
| 712 | +} |
| 713 | + |
| 714 | +static void retu_irq_ack(struct irq_data *data) |
| 715 | +{ |
| 716 | + struct retu *retu = irq_data_get_irq_chip_data(data); |
| 717 | + int irq = data->irq; |
| 718 | + |
| 719 | + retu->ack |= (1 << (irq - retu->irq_base)); |
| 720 | + retu->ack_pending = true; |
| 721 | +} |
| 722 | + |
| 723 | +static void retu_bus_lock(struct irq_data *data) |
| 724 | +{ |
| 725 | + struct retu *retu = irq_data_get_irq_chip_data(data); |
| 726 | + |
| 727 | + mutex_lock(&retu->mutex); |
| 728 | +} |
| 729 | + |
| 730 | +static void retu_bus_sync_unlock(struct irq_data *data) |
| 731 | +{ |
| 732 | + struct retu *retu = irq_data_get_irq_chip_data(data); |
| 733 | + |
| 734 | + if (retu->mask_pending) { |
| 735 | + __retu_write_reg(retu, RETU_REG_IMR, retu->mask); |
| 736 | + retu->mask_pending = false; |
| 737 | + } |
| 738 | + |
| 739 | + if (retu->ack_pending) { |
| 740 | + __retu_write_reg(retu, RETU_REG_IDR, retu->ack); |
| 741 | + retu->ack_pending = false; |
| 742 | + } |
| 743 | + |
| 744 | + mutex_unlock(&retu->mutex); |
| 745 | +} |
| 746 | + |
| 747 | +static struct irq_chip retu_irq_chip = { |
| 748 | + .name = "retu", |
| 749 | + .irq_bus_lock = retu_bus_lock, |
| 750 | + .irq_bus_sync_unlock = retu_bus_sync_unlock, |
| 751 | + .irq_mask = retu_irq_mask, |
| 752 | + .irq_unmask = retu_irq_unmask, |
| 753 | + .irq_ack = retu_irq_ack, |
| 754 | +}; |
| 755 | + |
| 756 | +static inline void retu_irq_setup(int irq) |
| 757 | +{ |
| 758 | +#ifdef CONFIG_ARM |
| 759 | + set_irq_flags(irq, IRQF_VALID); |
| 760 | +#else |
| 761 | + irq_set_noprobe(irq); |
| 762 | +#endif |
| 763 | +} |
| 764 | + |
| 765 | +static void retu_irq_init(struct retu *retu) |
| 766 | +{ |
| 767 | + int base = retu->irq_base; |
| 768 | + int end = retu->irq_end; |
| 769 | + int irq; |
| 770 | + |
| 771 | + for (irq = base; irq < end; irq++) { |
| 772 | + irq_set_chip_data(irq, retu); |
| 773 | + irq_set_chip_and_handler(irq, &retu_irq_chip, |
| 774 | + handle_simple_irq); |
| 775 | + irq_set_nested_thread(irq, 1); |
| 776 | + retu_irq_setup(irq); |
| 777 | + } |
| 778 | +} |
| 779 | + |
| 780 | +static void retu_irq_exit(struct retu *retu) |
| 781 | +{ |
| 782 | + int base = retu->irq_base; |
| 783 | + int end = retu->irq_end; |
| 784 | + int irq; |
| 785 | + |
| 786 | + for (irq = base; irq < end; irq++) { |
| 787 | +#ifdef CONFIG_ARM |
| 788 | + set_irq_flags(irq, 0); |
| 789 | +#endif |
| 790 | + irq_set_chip_and_handler(irq, NULL, NULL); |
| 791 | + irq_set_chip_data(irq, NULL); |
| 792 | + } |
| 793 | +} |
| 794 | + |
| 795 | +/* -------------------------------------------------------------------------- */ |
| 796 | + |
| 797 | +/** |
| 798 | + * retu_power_off - Shut down power to system |
| 799 | + * |
| 800 | + * This function puts the system in power off state |
| 801 | + */ |
| 802 | +static void retu_power_off(void) |
| 803 | +{ |
| 804 | + struct retu *retu = the_retu; |
| 805 | + unsigned reg; |
| 806 | + |
| 807 | + reg = __retu_read_reg(retu, RETU_REG_CC1); |
| 808 | + |
| 809 | + /* Ignore power button state */ |
| 810 | + __retu_write_reg(retu, RETU_REG_CC1, reg | 2); |
| 811 | + /* Expire watchdog immediately */ |
| 812 | + __retu_write_reg(retu, RETU_REG_WATCHDOG, 0); |
| 813 | + /* Wait for poweroff*/ |
| 814 | + for (;;); |
| 815 | +} |
| 816 | + |
| 817 | +static struct resource generic_resources[] = { |
| 818 | + { |
| 819 | + .start = -EINVAL, /* fixed later */ |
| 820 | + .flags = IORESOURCE_IRQ, |
| 821 | + }, |
| 822 | + { |
| 823 | + .start = -EINVAL, /* fixed later */ |
| 824 | + .flags = IORESOURCE_IRQ, |
| 825 | + }, |
| 826 | +}; |
| 827 | + |
| 828 | +/** |
| 829 | + * retu_allocate_child - Allocates one Retu child |
| 830 | + * @name: name of new child |
| 831 | + * @parent: parent device for this child |
| 832 | + */ |
| 833 | +static struct device *retu_allocate_child(char *name, struct device *parent, |
| 834 | + int irq_base, int irq1, int irq2, int num) |
| 835 | +{ |
| 836 | + struct platform_device *pdev; |
| 837 | + int status; |
| 838 | + |
| 839 | + pdev = platform_device_alloc(name, -1); |
| 840 | + if (!pdev) { |
| 841 | + dev_dbg(parent, "can't allocate %s\n", name); |
| 842 | + goto err; |
| 843 | + } |
| 844 | + |
| 845 | + pdev->dev.parent = parent; |
| 846 | + |
| 847 | + if (num) { |
| 848 | + generic_resources[0].start = irq_base + irq1; |
| 849 | + generic_resources[1].start = irq_base + irq2; |
| 850 | + |
| 851 | + status = platform_device_add_resources(pdev, |
| 852 | + generic_resources, num); |
| 853 | + if (status < 0) { |
| 854 | + dev_dbg(parent, "can't add resources to %s\n", name); |
| 855 | + goto err; |
| 856 | + } |
| 857 | + } |
| 858 | + |
| 859 | + status = platform_device_add(pdev); |
| 860 | + if (status < 0) { |
| 861 | + dev_dbg(parent, "can't add %s\n", name); |
| 862 | + goto err; |
| 863 | + } |
| 864 | + |
| 865 | + return &pdev->dev; |
| 866 | + |
| 867 | +err: |
| 868 | + platform_device_put(pdev); |
| 869 | + |
| 870 | + return NULL; |
| 871 | +} |
| 872 | + |
| 873 | +/** |
| 874 | + * retu_allocate_children - Allocates Retu's children |
| 875 | + */ |
| 876 | +static int retu_allocate_children(struct device *parent, int irq_base) |
| 877 | +{ |
| 878 | + struct device *child; |
| 879 | + |
| 880 | + child = retu_allocate_child("retu-pwrbutton", parent, irq_base, |
| 881 | + RETU_INT_PWR, -1, 1); |
| 882 | + if (!child) |
| 883 | + return -ENOMEM; |
| 884 | + |
| 885 | + child = retu_allocate_child("retu-headset", parent, irq_base, |
| 886 | + RETU_INT_HOOK, -1, 1); |
| 887 | + if (!child) |
| 888 | + return -ENOMEM; |
| 889 | + |
| 890 | + child = retu_allocate_child("retu-rtc", parent, irq_base, |
| 891 | + RETU_INT_RTCS, RETU_INT_RTCA, 2); |
| 892 | + if (!child) |
| 893 | + return -ENOMEM; |
| 894 | + |
| 895 | + child = retu_allocate_child("retu-wdt", parent, -1, -1, -1, 0); |
| 896 | + if (!child) |
| 897 | + return -ENOMEM; |
| 898 | + |
| 899 | + return 0; |
| 900 | +} |
| 901 | + |
| 902 | +/** |
| 903 | + * retu_probe - Probe for Retu ASIC |
| 904 | + * @dev: the Retu device |
| 905 | + * |
| 906 | + * Probe for the Retu ASIC and allocate memory |
| 907 | + * for its device-struct if found |
| 908 | + */ |
| 909 | +static int __devinit retu_probe(struct platform_device *pdev) |
| 910 | +{ |
| 911 | + struct retu *retu; |
| 912 | + struct cbus_retu_platform_data *pdata = pdev->dev.platform_data; |
| 913 | + |
| 914 | + int ret = -ENOMEM; |
| 915 | + int rev; |
| 916 | + |
| 917 | + retu = kzalloc(sizeof(*retu), GFP_KERNEL); |
| 918 | + if (!retu) { |
| 919 | + dev_err(&pdev->dev, "not enough memory\n"); |
| 920 | + goto err0; |
| 921 | + } |
| 922 | + |
| 923 | + platform_set_drvdata(pdev, retu); |
| 924 | + |
| 925 | + ret = irq_alloc_descs(-1, 0, MAX_RETU_IRQ_HANDLERS, 0); |
| 926 | + if (ret < 0) { |
| 927 | + dev_err(&pdev->dev, "failed to allocate IRQ descs\n"); |
| 928 | + goto err1; |
| 929 | + } |
| 930 | + |
| 931 | + retu->irq = platform_get_irq(pdev, 0); |
| 932 | + retu->irq_base = ret; |
| 933 | + retu->irq_end = ret + MAX_RETU_IRQ_HANDLERS; |
| 934 | + retu->devid = pdata->devid; |
| 935 | + retu->dev = &pdev->dev; |
| 936 | + the_retu = retu; |
| 937 | + |
| 938 | + mutex_init(&retu->mutex); |
| 939 | + |
| 940 | + retu_irq_init(retu); |
| 941 | + |
| 942 | + rev = __retu_read_reg(retu, RETU_REG_ASICR) & 0xff; |
| 943 | + if (rev & (1 << 7)) |
| 944 | + retu->is_vilma = true; |
| 945 | + |
| 946 | + dev_info(&pdev->dev, "%s v%d.%d found\n", |
| 947 | + retu->is_vilma ? "Vilma" : "Retu", |
| 948 | + (rev >> 4) & 0x07, rev & 0x0f); |
| 949 | + |
| 950 | + /* Mask all RETU interrupts */ |
| 951 | + __retu_write_reg(retu, RETU_REG_IMR, 0xffff); |
| 952 | + |
| 953 | + ret = request_threaded_irq(retu->irq, NULL, retu_irq_handler, |
| 954 | + IRQF_ONESHOT, "retu", retu); |
| 955 | + if (ret < 0) { |
| 956 | + dev_err(&pdev->dev, "Unable to register IRQ handler\n"); |
| 957 | + goto err2; |
| 958 | + } |
| 959 | + |
| 960 | + irq_set_irq_wake(retu->irq, 1); |
| 961 | + |
| 962 | + /* Register power off function */ |
| 963 | + pm_power_off = retu_power_off; |
| 964 | + |
| 965 | + ret = retu_allocate_children(&pdev->dev, retu->irq_base); |
| 966 | + if (ret < 0) { |
| 967 | + dev_err(&pdev->dev, "Unable to allocate Retu children\n"); |
| 968 | + goto err3; |
| 969 | + } |
| 970 | + |
| 971 | + return 0; |
| 972 | + |
| 973 | +err3: |
| 974 | + pm_power_off = NULL; |
| 975 | + free_irq(retu->irq, retu); |
| 976 | + |
| 977 | +err2: |
| 978 | + retu_irq_exit(retu); |
| 979 | + irq_free_descs(retu->irq_base, MAX_RETU_IRQ_HANDLERS); |
| 980 | + |
| 981 | +err1: |
| 982 | + kfree(retu); |
| 983 | + the_retu = NULL; |
| 984 | + |
| 985 | +err0: |
| 986 | + return ret; |
| 987 | +} |
| 988 | + |
| 989 | +static int __devexit retu_remove(struct platform_device *pdev) |
| 990 | +{ |
| 991 | + struct retu *retu = platform_get_drvdata(pdev); |
| 992 | + |
| 993 | + pm_power_off = NULL; |
| 994 | + the_retu = NULL; |
| 995 | + |
| 996 | + free_irq(retu->irq, retu); |
| 997 | + retu_irq_exit(retu); |
| 998 | + irq_free_descs(retu->irq_base, MAX_RETU_IRQ_HANDLERS); |
| 999 | + kfree(retu); |
| 1000 | + |
| 1001 | + return 0; |
| 1002 | +} |
| 1003 | + |
| 1004 | +static struct platform_driver retu_driver = { |
| 1005 | + .probe = retu_probe, |
| 1006 | + .remove = __devexit_p(retu_remove), |
| 1007 | + .driver = { |
| 1008 | + .name = "retu", |
| 1009 | + }, |
| 1010 | +}; |
| 1011 | + |
| 1012 | +static int __init retu_init(void) |
| 1013 | +{ |
| 1014 | + return platform_driver_register(&retu_driver); |
| 1015 | +} |
| 1016 | +subsys_initcall(retu_init); |
| 1017 | + |
| 1018 | +static void __exit retu_exit(void) |
| 1019 | +{ |
| 1020 | + platform_driver_unregister(&retu_driver); |
| 1021 | +} |
| 1022 | +module_exit(retu_exit); |
| 1023 | + |
| 1024 | +MODULE_DESCRIPTION("Retu ASIC control"); |
| 1025 | +MODULE_LICENSE("GPL"); |
| 1026 | +MODULE_AUTHOR("Juha Yrjölä"); |
| 1027 | +MODULE_AUTHOR("David Weinehall"); |
| 1028 | +MODULE_AUTHOR("Mikko Ylinen"); |
| 1029 | --- /dev/null |
| 1030 | +++ b/drivers/cbus/retu.h |
| 1031 | @@ -0,0 +1,85 @@ |
| 1032 | +/** |
| 1033 | + * drivers/cbus/retu.h |
| 1034 | + * |
| 1035 | + * Copyright (C) 2004, 2005 Nokia Corporation |
| 1036 | + * |
| 1037 | + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and |
| 1038 | + * David Weinehall <david.weinehall@nokia.com> |
| 1039 | + * |
| 1040 | + * This file is subject to the terms and conditions of the GNU General |
| 1041 | + * Public License. See the file "COPYING" in the main directory of this |
| 1042 | + * archive for more details. |
| 1043 | + * |
| 1044 | + * This program is distributed in the hope that it will be useful, |
| 1045 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 1046 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 1047 | + * GNU General Public License for more details. |
| 1048 | + |
| 1049 | + * You should have received a copy of the GNU General Public License |
| 1050 | + * along with this program; if not, write to the Free Software |
| 1051 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 1052 | + */ |
| 1053 | + |
| 1054 | +#ifndef __DRIVERS_CBUS_RETU_H |
| 1055 | +#define __DRIVERS_CBUS_RETU_H |
| 1056 | + |
| 1057 | +#include <linux/types.h> |
| 1058 | + |
| 1059 | +/* Registers */ |
| 1060 | +#define RETU_REG_ASICR 0x00 /* ASIC ID & revision */ |
| 1061 | +#define RETU_REG_IDR 0x01 /* Interrupt ID */ |
| 1062 | +#define RETU_REG_IMR 0x02 /* Interrupt mask */ |
| 1063 | +#define RETU_REG_RTCDSR 0x03 /* RTC seconds register */ |
| 1064 | +#define RETU_REG_RTCHMR 0x04 /* RTC hours and minutes register */ |
| 1065 | +#define RETU_REG_RTCHMAR 0x05 /* RTC hours and minutes alarm and time set register */ |
| 1066 | +#define RETU_REG_RTCCALR 0x06 /* RTC calibration register */ |
| 1067 | +#define RETU_REG_ADCR 0x08 /* ADC result */ |
| 1068 | +#define RETU_REG_ADCSCR 0x09 /* ADC sample ctrl */ |
| 1069 | +#define RETU_REG_CC1 0x0d /* Common control register 1 */ |
| 1070 | +#define RETU_REG_CC2 0x0e /* Common control register 2 */ |
| 1071 | +#define RETU_REG_CTRL_CLR 0x0f /* Regulator clear register */ |
| 1072 | +#define RETU_REG_CTRL_SET 0x10 /* Regulator set register */ |
| 1073 | +#define RETU_REG_STATUS 0x16 /* Status register */ |
| 1074 | +#define RETU_REG_STATUS_BATAVAIL 0x0100 /* Battery available */ |
| 1075 | +#define RETU_REG_STATUS_CHGPLUG 0x1000 /* Charger is plugged in */ |
| 1076 | +#define RETU_REG_WATCHDOG 0x17 /* Watchdog register */ |
| 1077 | +#define RETU_REG_AUDTXR 0x18 /* Audio Codec Tx register */ |
| 1078 | +#define RETU_REG_MAX 0x1f |
| 1079 | + |
| 1080 | +/* Interrupt sources */ |
| 1081 | +#define RETU_INT_PWR 0 |
| 1082 | +#define RETU_INT_CHAR 1 |
| 1083 | +#define RETU_INT_RTCS 2 |
| 1084 | +#define RETU_INT_RTCM 3 |
| 1085 | +#define RETU_INT_RTCD 4 |
| 1086 | +#define RETU_INT_RTCA 5 |
| 1087 | +#define RETU_INT_HOOK 6 |
| 1088 | +#define RETU_INT_HEAD 7 |
| 1089 | +#define RETU_INT_ADCS 8 |
| 1090 | + |
| 1091 | +#define MAX_RETU_IRQ_HANDLERS 16 |
| 1092 | + |
| 1093 | +/* ADC channels */ |
| 1094 | +#define RETU_ADC_GND 0x00 /* Ground */ |
| 1095 | +#define RETU_ADC_BSI 0x01 /* Battery Size Indicator */ |
| 1096 | +#define RETU_ADC_BATTEMP 0x02 /* Battery temperature */ |
| 1097 | +#define RETU_ADC_CHGVOLT 0x03 /* Charger voltage */ |
| 1098 | +#define RETU_ADC_HEADSET 0x04 /* Headset detection */ |
| 1099 | +#define RETU_ADC_HOOKDET 0x05 /* Hook detection */ |
| 1100 | +#define RETU_ADC_RFGP 0x06 /* RF GP */ |
| 1101 | +#define RETU_ADC_WBTX 0x07 /* Wideband Tx detection */ |
| 1102 | +#define RETU_ADC_BATTVOLT 0x08 /* Battery voltage measurement */ |
| 1103 | +#define RETU_ADC_GND2 0x09 /* Ground */ |
| 1104 | +#define RETU_ADC_LIGHTSENS 0x0A /* Light sensor */ |
| 1105 | +#define RETU_ADC_LIGHTTEMP 0x0B /* Light sensor temperature */ |
| 1106 | +#define RETU_ADC_BKUPVOLT 0x0C /* Backup battery voltage */ |
| 1107 | +#define RETU_ADC_TEMP 0x0D /* RETU temperature */ |
| 1108 | + |
| 1109 | + |
| 1110 | +int retu_read_reg(struct device *child, unsigned reg); |
| 1111 | +void retu_write_reg(struct device *child, unsigned reg, u16 val); |
| 1112 | +void retu_set_clear_reg_bits(struct device *child, unsigned reg, u16 set, |
| 1113 | + u16 clear); |
| 1114 | +int retu_read_adc(struct device *child, int channel); |
| 1115 | + |
| 1116 | +#endif /* __DRIVERS_CBUS_RETU_H */ |
| 1117 | --- /dev/null |
| 1118 | +++ b/drivers/cbus/retu-headset.c |
| 1119 | @@ -0,0 +1,359 @@ |
| 1120 | +/** |
| 1121 | + * Retu/Vilma headset detection |
| 1122 | + * |
| 1123 | + * Copyright (C) 2006 Nokia Corporation |
| 1124 | + * |
| 1125 | + * Written by Juha Yrjölä |
| 1126 | + * |
| 1127 | + * This file is subject to the terms and conditions of the GNU General |
| 1128 | + * Public License. See the file "COPYING" in the main directory of this |
| 1129 | + * archive for more details. |
| 1130 | + * |
| 1131 | + * This program is distributed in the hope that it will be useful, |
| 1132 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 1133 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 1134 | + * GNU General Public License for more details. |
| 1135 | + * |
| 1136 | + * You should have received a copy of the GNU General Public License |
| 1137 | + * along with this program; if not, write to the Free Software |
| 1138 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 1139 | + */ |
| 1140 | + |
| 1141 | +#include <linux/module.h> |
| 1142 | +#include <linux/init.h> |
| 1143 | +#include <linux/kernel.h> |
| 1144 | +#include <linux/irq.h> |
| 1145 | +#include <linux/interrupt.h> |
| 1146 | +#include <linux/slab.h> |
| 1147 | +#include <linux/delay.h> |
| 1148 | +#include <linux/input.h> |
| 1149 | +#include <linux/platform_device.h> |
| 1150 | + |
| 1151 | +#include "retu.h" |
| 1152 | + |
| 1153 | +#define RETU_ADC_CHANNEL_HOOKDET 0x05 |
| 1154 | + |
| 1155 | +#define RETU_HEADSET_KEY KEY_PHONE |
| 1156 | + |
| 1157 | +struct retu_headset { |
| 1158 | + spinlock_t lock; |
| 1159 | + struct mutex mutex; |
| 1160 | + struct device *dev; |
| 1161 | + struct input_dev *idev; |
| 1162 | + unsigned bias_enabled; |
| 1163 | + unsigned detection_enabled; |
| 1164 | + unsigned pressed; |
| 1165 | + struct timer_list enable_timer; |
| 1166 | + struct timer_list detect_timer; |
| 1167 | + int irq; |
| 1168 | +}; |
| 1169 | + |
| 1170 | +static void retu_headset_set_bias(struct retu_headset *hs, int enable) |
| 1171 | +{ |
| 1172 | + if (enable) { |
| 1173 | + retu_set_clear_reg_bits(hs->dev, RETU_REG_AUDTXR, |
| 1174 | + (1 << 0) | (1 << 1), 0); |
| 1175 | + msleep(2); |
| 1176 | + retu_set_clear_reg_bits(hs->dev, RETU_REG_AUDTXR, |
| 1177 | + 1 << 3, 0); |
| 1178 | + } else { |
| 1179 | + retu_set_clear_reg_bits(hs->dev, RETU_REG_AUDTXR, 0, |
| 1180 | + (1 << 0) | (1 << 1) | (1 << 3)); |
| 1181 | + } |
| 1182 | +} |
| 1183 | + |
| 1184 | +static void retu_headset_enable(struct retu_headset *hs) |
| 1185 | +{ |
| 1186 | + mutex_lock(&hs->mutex); |
| 1187 | + if (!hs->bias_enabled) { |
| 1188 | + hs->bias_enabled = 1; |
| 1189 | + retu_headset_set_bias(hs, 1); |
| 1190 | + } |
| 1191 | + mutex_unlock(&hs->mutex); |
| 1192 | +} |
| 1193 | + |
| 1194 | +static void retu_headset_disable(struct retu_headset *hs) |
| 1195 | +{ |
| 1196 | + mutex_lock(&hs->mutex); |
| 1197 | + if (hs->bias_enabled) { |
| 1198 | + hs->bias_enabled = 0; |
| 1199 | + retu_headset_set_bias(hs, 0); |
| 1200 | + } |
| 1201 | + mutex_unlock(&hs->mutex); |
| 1202 | +} |
| 1203 | + |
| 1204 | +static void retu_headset_det_enable(struct retu_headset *hs) |
| 1205 | +{ |
| 1206 | + mutex_lock(&hs->mutex); |
| 1207 | + if (!hs->detection_enabled) { |
| 1208 | + hs->detection_enabled = 1; |
| 1209 | + retu_set_clear_reg_bits(hs->dev, RETU_REG_CC1, |
| 1210 | + (1 << 10) | (1 << 8), 0); |
| 1211 | + } |
| 1212 | + mutex_unlock(&hs->mutex); |
| 1213 | +} |
| 1214 | + |
| 1215 | +static void retu_headset_det_disable(struct retu_headset *hs) |
| 1216 | +{ |
| 1217 | + unsigned long flags; |
| 1218 | + |
| 1219 | + mutex_lock(&hs->mutex); |
| 1220 | + if (hs->detection_enabled) { |
| 1221 | + hs->detection_enabled = 0; |
| 1222 | + del_timer_sync(&hs->enable_timer); |
| 1223 | + del_timer_sync(&hs->detect_timer); |
| 1224 | + spin_lock_irqsave(&hs->lock, flags); |
| 1225 | + if (hs->pressed) |
| 1226 | + input_report_key(hs->idev, RETU_HEADSET_KEY, 0); |
| 1227 | + spin_unlock_irqrestore(&hs->lock, flags); |
| 1228 | + retu_set_clear_reg_bits(hs->dev, RETU_REG_CC1, 0, |
| 1229 | + (1 << 10) | (1 << 8)); |
| 1230 | + } |
| 1231 | + mutex_unlock(&hs->mutex); |
| 1232 | +} |
| 1233 | + |
| 1234 | +static ssize_t retu_headset_hookdet_show(struct device *dev, |
| 1235 | + struct device_attribute *attr, |
| 1236 | + char *buf) |
| 1237 | +{ |
| 1238 | + int val; |
| 1239 | + |
| 1240 | + val = retu_read_adc(dev, RETU_ADC_CHANNEL_HOOKDET); |
| 1241 | + return sprintf(buf, "%d\n", val); |
| 1242 | +} |
| 1243 | + |
| 1244 | +static DEVICE_ATTR(hookdet, S_IRUGO, retu_headset_hookdet_show, NULL); |
| 1245 | + |
| 1246 | +static ssize_t retu_headset_enable_show(struct device *dev, |
| 1247 | + struct device_attribute *attr, |
| 1248 | + char *buf) |
| 1249 | +{ |
| 1250 | + struct retu_headset *hs = dev_get_drvdata(dev); |
| 1251 | + |
| 1252 | + return sprintf(buf, "%u\n", hs->bias_enabled); |
| 1253 | +} |
| 1254 | + |
| 1255 | +static ssize_t retu_headset_enable_store(struct device *dev, |
| 1256 | + struct device_attribute *attr, |
| 1257 | + const char *buf, size_t count) |
| 1258 | +{ |
| 1259 | + struct retu_headset *hs = dev_get_drvdata(dev); |
| 1260 | + int enable; |
| 1261 | + |
| 1262 | + if (sscanf(buf, "%u", &enable) != 1) |
| 1263 | + return -EINVAL; |
| 1264 | + if (enable) |
| 1265 | + retu_headset_enable(hs); |
| 1266 | + else |
| 1267 | + retu_headset_disable(hs); |
| 1268 | + return count; |
| 1269 | +} |
| 1270 | + |
| 1271 | +static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWGRP, |
| 1272 | + retu_headset_enable_show, retu_headset_enable_store); |
| 1273 | + |
| 1274 | +static ssize_t retu_headset_enable_det_show(struct device *dev, |
| 1275 | + struct device_attribute *attr, |
| 1276 | + char *buf) |
| 1277 | +{ |
| 1278 | + struct retu_headset *hs = dev_get_drvdata(dev); |
| 1279 | + |
| 1280 | + return sprintf(buf, "%u\n", hs->detection_enabled); |
| 1281 | +} |
| 1282 | + |
| 1283 | +static ssize_t retu_headset_enable_det_store(struct device *dev, |
| 1284 | + struct device_attribute *attr, |
| 1285 | + const char *buf, size_t count) |
| 1286 | +{ |
| 1287 | + struct retu_headset *hs = dev_get_drvdata(dev); |
| 1288 | + int enable; |
| 1289 | + |
| 1290 | + if (sscanf(buf, "%u", &enable) != 1) |
| 1291 | + return -EINVAL; |
| 1292 | + if (enable) |
| 1293 | + retu_headset_det_enable(hs); |
| 1294 | + else |
| 1295 | + retu_headset_det_disable(hs); |
| 1296 | + return count; |
| 1297 | +} |
| 1298 | + |
| 1299 | +static DEVICE_ATTR(enable_det, S_IRUGO | S_IWUSR | S_IWGRP, |
| 1300 | + retu_headset_enable_det_show, |
| 1301 | + retu_headset_enable_det_store); |
| 1302 | + |
| 1303 | +static irqreturn_t retu_headset_hook_interrupt(int irq, void *_hs) |
| 1304 | +{ |
| 1305 | + struct retu_headset *hs = _hs; |
| 1306 | + unsigned long flags; |
| 1307 | + |
| 1308 | + spin_lock_irqsave(&hs->lock, flags); |
| 1309 | + if (!hs->pressed) { |
| 1310 | + /* Headset button was just pressed down. */ |
| 1311 | + hs->pressed = 1; |
| 1312 | + input_report_key(hs->idev, RETU_HEADSET_KEY, 1); |
| 1313 | + } |
| 1314 | + spin_unlock_irqrestore(&hs->lock, flags); |
| 1315 | + retu_set_clear_reg_bits(hs->dev, RETU_REG_CC1, 0, |
| 1316 | + (1 << 10) | (1 << 8)); |
| 1317 | + mod_timer(&hs->enable_timer, jiffies + msecs_to_jiffies(50)); |
| 1318 | + |
| 1319 | + return IRQ_HANDLED; |
| 1320 | +} |
| 1321 | + |
| 1322 | +static void retu_headset_enable_timer(unsigned long arg) |
| 1323 | +{ |
| 1324 | + struct retu_headset *hs = (struct retu_headset *) arg; |
| 1325 | + |
| 1326 | + retu_set_clear_reg_bits(hs->dev, RETU_REG_CC1, |
| 1327 | + (1 << 10) | (1 << 8), 0); |
| 1328 | + mod_timer(&hs->detect_timer, jiffies + msecs_to_jiffies(350)); |
| 1329 | +} |
| 1330 | + |
| 1331 | +static void retu_headset_detect_timer(unsigned long arg) |
| 1332 | +{ |
| 1333 | + struct retu_headset *hs = (struct retu_headset *) arg; |
| 1334 | + unsigned long flags; |
| 1335 | + |
| 1336 | + spin_lock_irqsave(&hs->lock, flags); |
| 1337 | + if (hs->pressed) { |
| 1338 | + hs->pressed = 0; |
| 1339 | + input_report_key(hs->idev, RETU_HEADSET_KEY, 0); |
| 1340 | + } |
| 1341 | + spin_unlock_irqrestore(&hs->lock, flags); |
| 1342 | +} |
| 1343 | + |
| 1344 | +static int __init retu_headset_probe(struct platform_device *pdev) |
| 1345 | +{ |
| 1346 | + struct retu_headset *hs; |
| 1347 | + int irq; |
| 1348 | + int r; |
| 1349 | + |
| 1350 | + hs = kzalloc(sizeof(*hs), GFP_KERNEL); |
| 1351 | + if (hs == NULL) |
| 1352 | + return -ENOMEM; |
| 1353 | + |
| 1354 | + hs->dev = &pdev->dev; |
| 1355 | + |
| 1356 | + hs->idev = input_allocate_device(); |
| 1357 | + if (hs->idev == NULL) { |
| 1358 | + r = -ENOMEM; |
| 1359 | + goto err1; |
| 1360 | + } |
| 1361 | + hs->idev->name = "retu-headset"; |
| 1362 | + hs->idev->dev.parent = &pdev->dev; |
| 1363 | + set_bit(EV_KEY, hs->idev->evbit); |
| 1364 | + set_bit(RETU_HEADSET_KEY, hs->idev->keybit); |
| 1365 | + r = input_register_device(hs->idev); |
| 1366 | + if (r < 0) |
| 1367 | + goto err2; |
| 1368 | + |
| 1369 | + r = device_create_file(&pdev->dev, &dev_attr_hookdet); |
| 1370 | + if (r < 0) |
| 1371 | + goto err3; |
| 1372 | + r = device_create_file(&pdev->dev, &dev_attr_enable); |
| 1373 | + if (r < 0) |
| 1374 | + goto err4; |
| 1375 | + r = device_create_file(&pdev->dev, &dev_attr_enable_det); |
| 1376 | + if (r < 0) |
| 1377 | + goto err5; |
| 1378 | + platform_set_drvdata(pdev, hs); |
| 1379 | + |
| 1380 | + spin_lock_init(&hs->lock); |
| 1381 | + mutex_init(&hs->mutex); |
| 1382 | + setup_timer(&hs->enable_timer, retu_headset_enable_timer, |
| 1383 | + (unsigned long) hs); |
| 1384 | + setup_timer(&hs->detect_timer, retu_headset_detect_timer, |
| 1385 | + (unsigned long) hs); |
| 1386 | + |
| 1387 | + irq = platform_get_irq(pdev, 0); |
| 1388 | + hs->irq = irq; |
| 1389 | + |
| 1390 | + r = request_threaded_irq(irq, NULL, retu_headset_hook_interrupt, 0, |
| 1391 | + "hookdet", hs); |
| 1392 | + if (r != 0) { |
| 1393 | + dev_err(&pdev->dev, "hookdet IRQ not available\n"); |
| 1394 | + goto err6; |
| 1395 | + } |
| 1396 | + |
| 1397 | + return 0; |
| 1398 | +err6: |
| 1399 | + device_remove_file(&pdev->dev, &dev_attr_enable_det); |
| 1400 | +err5: |
| 1401 | + device_remove_file(&pdev->dev, &dev_attr_enable); |
| 1402 | +err4: |
| 1403 | + device_remove_file(&pdev->dev, &dev_attr_hookdet); |
| 1404 | +err3: |
| 1405 | + input_unregister_device(hs->idev); |
| 1406 | +err2: |
| 1407 | + input_free_device(hs->idev); |
| 1408 | +err1: |
| 1409 | + kfree(hs); |
| 1410 | + return r; |
| 1411 | +} |
| 1412 | + |
| 1413 | +static int retu_headset_remove(struct platform_device *pdev) |
| 1414 | +{ |
| 1415 | + struct retu_headset *hs = platform_get_drvdata(pdev); |
| 1416 | + |
| 1417 | + device_remove_file(&pdev->dev, &dev_attr_hookdet); |
| 1418 | + device_remove_file(&pdev->dev, &dev_attr_enable); |
| 1419 | + device_remove_file(&pdev->dev, &dev_attr_enable_det); |
| 1420 | + retu_headset_disable(hs); |
| 1421 | + retu_headset_det_disable(hs); |
| 1422 | + free_irq(hs->irq, hs); |
| 1423 | + input_unregister_device(hs->idev); |
| 1424 | + input_free_device(hs->idev); |
| 1425 | + |
| 1426 | + return 0; |
| 1427 | +} |
| 1428 | + |
| 1429 | +static int retu_headset_suspend(struct platform_device *pdev, |
| 1430 | + pm_message_t mesg) |
| 1431 | +{ |
| 1432 | + struct retu_headset *hs = platform_get_drvdata(pdev); |
| 1433 | + |
| 1434 | + mutex_lock(&hs->mutex); |
| 1435 | + if (hs->bias_enabled) |
| 1436 | + retu_headset_set_bias(hs, 0); |
| 1437 | + mutex_unlock(&hs->mutex); |
| 1438 | + |
| 1439 | + return 0; |
| 1440 | +} |
| 1441 | + |
| 1442 | +static int retu_headset_resume(struct platform_device *pdev) |
| 1443 | +{ |
| 1444 | + struct retu_headset *hs = platform_get_drvdata(pdev); |
| 1445 | + |
| 1446 | + mutex_lock(&hs->mutex); |
| 1447 | + if (hs->bias_enabled) |
| 1448 | + retu_headset_set_bias(hs, 1); |
| 1449 | + mutex_unlock(&hs->mutex); |
| 1450 | + |
| 1451 | + return 0; |
| 1452 | +} |
| 1453 | + |
| 1454 | +static struct platform_driver retu_headset_driver = { |
| 1455 | + .remove = retu_headset_remove, |
| 1456 | + .suspend = retu_headset_suspend, |
| 1457 | + .resume = retu_headset_resume, |
| 1458 | + .driver = { |
| 1459 | + .name = "retu-headset", |
| 1460 | + }, |
| 1461 | +}; |
| 1462 | + |
| 1463 | +static int __init retu_headset_init(void) |
| 1464 | +{ |
| 1465 | + return platform_driver_probe(&retu_headset_driver, retu_headset_probe); |
| 1466 | +} |
| 1467 | + |
| 1468 | +static void __exit retu_headset_exit(void) |
| 1469 | +{ |
| 1470 | + platform_driver_unregister(&retu_headset_driver); |
| 1471 | +} |
| 1472 | + |
| 1473 | +module_init(retu_headset_init); |
| 1474 | +module_exit(retu_headset_exit); |
| 1475 | + |
| 1476 | +MODULE_DESCRIPTION("Retu/Vilma headset detection"); |
| 1477 | +MODULE_LICENSE("GPL"); |
| 1478 | +MODULE_AUTHOR("Juha Yrjölä"); |
| 1479 | --- /dev/null |
| 1480 | +++ b/drivers/cbus/retu-pwrbutton.c |
| 1481 | @@ -0,0 +1,165 @@ |
| 1482 | +/** |
| 1483 | + * drivers/cbus/retu-pwrbutton.c |
| 1484 | + * |
| 1485 | + * Driver for sending retu power button event to input-layer |
| 1486 | + * |
| 1487 | + * Copyright (C) 2004-2010 Nokia Corporation |
| 1488 | + * |
| 1489 | + * Written by |
| 1490 | + * Ari Saastamoinen <ari.saastamoinen@elektrobit.com> |
| 1491 | + * Juha Yrjola <juha.yrjola@solidboot.com> |
| 1492 | + * |
| 1493 | + * Contact: Felipe Balbi <felipe.balbi@nokia.com> |
| 1494 | + * |
| 1495 | + * This file is subject to the terms and conditions of the GNU General |
| 1496 | + * Public License. See the file "COPYING" in the main directory of this |
| 1497 | + * archive for more details. |
| 1498 | + * |
| 1499 | + * This program is distributed in the hope that it will be useful, |
| 1500 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 1501 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 1502 | + * GNU General Public License for more details. |
| 1503 | + * |
| 1504 | + * You should have received a copy of the GNU General Public License |
| 1505 | + * along with this program; if not, write to the Free Software |
| 1506 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 1507 | + */ |
| 1508 | + |
| 1509 | +#include <linux/module.h> |
| 1510 | +#include <linux/init.h> |
| 1511 | +#include <linux/kernel.h> |
| 1512 | +#include <linux/errno.h> |
| 1513 | +#include <linux/input.h> |
| 1514 | +#include <linux/jiffies.h> |
| 1515 | +#include <linux/bitops.h> |
| 1516 | +#include <linux/irq.h> |
| 1517 | +#include <linux/interrupt.h> |
| 1518 | +#include <linux/platform_device.h> |
| 1519 | +#include <linux/slab.h> |
| 1520 | + |
| 1521 | +#include "retu.h" |
| 1522 | + |
| 1523 | +#define RETU_STATUS_PWRONX (1 << 5) |
| 1524 | + |
| 1525 | +#define PWRBTN_DELAY 20 |
| 1526 | +#define PWRBTN_UP 0 |
| 1527 | +#define PWRBTN_PRESSED 1 |
| 1528 | + |
| 1529 | +struct retu_pwrbutton { |
| 1530 | + struct input_dev *idev; |
| 1531 | + struct device *dev; |
| 1532 | + |
| 1533 | + int state; |
| 1534 | + int irq; |
| 1535 | +}; |
| 1536 | + |
| 1537 | +static irqreturn_t retubutton_irq(int irq, void *_pwr) |
| 1538 | +{ |
| 1539 | + struct retu_pwrbutton *pwr = _pwr; |
| 1540 | + int state; |
| 1541 | + |
| 1542 | + if (retu_read_reg(pwr->dev, RETU_REG_STATUS) & RETU_STATUS_PWRONX) |
| 1543 | + state = PWRBTN_UP; |
| 1544 | + else |
| 1545 | + state = PWRBTN_PRESSED; |
| 1546 | + |
| 1547 | + if (pwr->state != state) { |
| 1548 | + input_report_key(pwr->idev, KEY_POWER, state); |
| 1549 | + input_sync(pwr->idev); |
| 1550 | + pwr->state = state; |
| 1551 | + } |
| 1552 | + |
| 1553 | + return IRQ_HANDLED; |
| 1554 | +} |
| 1555 | + |
| 1556 | +static int __init retubutton_probe(struct platform_device *pdev) |
| 1557 | +{ |
| 1558 | + struct retu_pwrbutton *pwr; |
| 1559 | + int ret = 0; |
| 1560 | + |
| 1561 | + pwr = kzalloc(sizeof(*pwr), GFP_KERNEL); |
| 1562 | + if (!pwr) { |
| 1563 | + dev_err(&pdev->dev, "not enough memory\n"); |
| 1564 | + ret = -ENOMEM; |
| 1565 | + goto err0; |
| 1566 | + } |
| 1567 | + |
| 1568 | + pwr->dev = &pdev->dev; |
| 1569 | + pwr->irq = platform_get_irq(pdev, 0); |
| 1570 | + platform_set_drvdata(pdev, pwr); |
| 1571 | + |
| 1572 | + ret = request_threaded_irq(pwr->irq, NULL, retubutton_irq, 0, |
| 1573 | + "retu-pwrbutton", pwr); |
| 1574 | + if (ret < 0) { |
| 1575 | + dev_err(&pdev->dev, "Cannot allocate irq\n"); |
| 1576 | + goto err1; |
| 1577 | + } |
| 1578 | + |
| 1579 | + pwr->idev = input_allocate_device(); |
| 1580 | + if (!pwr->idev) { |
| 1581 | + dev_err(&pdev->dev, "can't allocate input device\n"); |
| 1582 | + ret = -ENOMEM; |
| 1583 | + goto err2; |
| 1584 | + } |
| 1585 | + |
| 1586 | + pwr->idev->evbit[0] = BIT_MASK(EV_KEY); |
| 1587 | + pwr->idev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER); |
| 1588 | + pwr->idev->name = "retu-pwrbutton"; |
| 1589 | + |
| 1590 | + ret = input_register_device(pwr->idev); |
| 1591 | + if (ret < 0) { |
| 1592 | + dev_err(&pdev->dev, "failed to register input device\n"); |
| 1593 | + goto err3; |
| 1594 | + } |
| 1595 | + |
| 1596 | + return 0; |
| 1597 | + |
| 1598 | +err3: |
| 1599 | + input_free_device(pwr->idev); |
| 1600 | + |
| 1601 | +err2: |
| 1602 | + free_irq(pwr->irq, pwr); |
| 1603 | + |
| 1604 | +err1: |
| 1605 | + kfree(pwr); |
| 1606 | + |
| 1607 | +err0: |
| 1608 | + return ret; |
| 1609 | +} |
| 1610 | + |
| 1611 | +static int __exit retubutton_remove(struct platform_device *pdev) |
| 1612 | +{ |
| 1613 | + struct retu_pwrbutton *pwr = platform_get_drvdata(pdev); |
| 1614 | + |
| 1615 | + free_irq(pwr->irq, pwr); |
| 1616 | + input_unregister_device(pwr->idev); |
| 1617 | + input_free_device(pwr->idev); |
| 1618 | + kfree(pwr); |
| 1619 | + |
| 1620 | + return 0; |
| 1621 | +} |
| 1622 | + |
| 1623 | +static struct platform_driver retu_pwrbutton_driver = { |
| 1624 | + .remove = __exit_p(retubutton_remove), |
| 1625 | + .driver = { |
| 1626 | + .name = "retu-pwrbutton", |
| 1627 | + }, |
| 1628 | +}; |
| 1629 | + |
| 1630 | +static int __init retubutton_init(void) |
| 1631 | +{ |
| 1632 | + return platform_driver_probe(&retu_pwrbutton_driver, retubutton_probe); |
| 1633 | +} |
| 1634 | +module_init(retubutton_init); |
| 1635 | + |
| 1636 | +static void __exit retubutton_exit(void) |
| 1637 | +{ |
| 1638 | + platform_driver_unregister(&retu_pwrbutton_driver); |
| 1639 | +} |
| 1640 | +module_exit(retubutton_exit); |
| 1641 | + |
| 1642 | +MODULE_DESCRIPTION("Retu Power Button"); |
| 1643 | +MODULE_LICENSE("GPL"); |
| 1644 | +MODULE_AUTHOR("Ari Saastamoinen"); |
| 1645 | +MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>"); |
| 1646 | + |
| 1647 | --- /dev/null |
| 1648 | +++ b/drivers/cbus/retu-rtc.c |
| 1649 | @@ -0,0 +1,287 @@ |
| 1650 | +/** |
| 1651 | + * drivers/cbus/retu-rtc.c |
| 1652 | + * |
| 1653 | + * Support for Retu RTC |
| 1654 | + * |
| 1655 | + * Copyright (C) 2004, 2005 Nokia Corporation |
| 1656 | + * |
| 1657 | + * Written by Paul Mundt <paul.mundt@nokia.com> and |
| 1658 | + * Igor Stoppa <igor.stoppa@nokia.com> |
| 1659 | + * |
| 1660 | + * The Retu RTC is essentially a partial read-only RTC that gives us Retu's |
| 1661 | + * idea of what time actually is. It's left as a userspace excercise to map |
| 1662 | + * this back to time in the real world and ensure that calibration settings |
| 1663 | + * are sane to compensate for any horrible drift (on account of not being able |
| 1664 | + * to set the clock to anything). |
| 1665 | + * |
| 1666 | + * Days are semi-writeable. Namely, Retu will only track 255 days for us |
| 1667 | + * consecutively, after which the counter is explicitly stuck at 255 until |
| 1668 | + * someone comes along and clears it with a write. In the event that no one |
| 1669 | + * comes along and clears it, we no longer have any idea what day it is. |
| 1670 | + * |
| 1671 | + * This file is subject to the terms and conditions of the GNU General |
| 1672 | + * Public License. See the file "COPYING" in the main directory of this |
| 1673 | + * archive for more details. |
| 1674 | + * |
| 1675 | + * This program is distributed in the hope that it will be useful, |
| 1676 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 1677 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 1678 | + * GNU General Public License for more details. |
| 1679 | + * |
| 1680 | + * You should have received a copy of the GNU General Public License |
| 1681 | + * along with this program; if not, write to the Free Software |
| 1682 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 1683 | + */ |
| 1684 | + |
| 1685 | +#include <linux/device.h> |
| 1686 | +#include <linux/init.h> |
| 1687 | +#include <linux/kernel.h> |
| 1688 | +#include <linux/slab.h> |
| 1689 | +#include <linux/module.h> |
| 1690 | +#include <linux/platform_device.h> |
| 1691 | +#include <linux/mutex.h> |
| 1692 | +#include <linux/rtc.h> |
| 1693 | + |
| 1694 | +#include "cbus.h" |
| 1695 | +#include "retu.h" |
| 1696 | + |
| 1697 | +struct retu_rtc { |
| 1698 | + /* device lock */ |
| 1699 | + struct mutex mutex; |
| 1700 | + struct device *dev; |
| 1701 | + struct rtc_device *rtc; |
| 1702 | + |
| 1703 | + u16 alarm_expired; |
| 1704 | + int irq_rtcs; |
| 1705 | + int irq_rtca; |
| 1706 | +}; |
| 1707 | + |
| 1708 | +static void retu_rtc_do_reset(struct retu_rtc *rtc) |
| 1709 | +{ |
| 1710 | + u16 ccr1; |
| 1711 | + |
| 1712 | + ccr1 = retu_read_reg(rtc->dev, RETU_REG_CC1); |
| 1713 | + /* RTC in reset */ |
| 1714 | + retu_write_reg(rtc->dev, RETU_REG_CC1, ccr1 | 0x0001); |
| 1715 | + /* RTC in normal operating mode */ |
| 1716 | + retu_write_reg(rtc->dev, RETU_REG_CC1, ccr1 & ~0x0001); |
| 1717 | + |
| 1718 | + /* Disable alarm and RTC WD */ |
| 1719 | + retu_write_reg(rtc->dev, RETU_REG_RTCHMAR, 0x7f3f); |
| 1720 | + /* Set Calibration register to default value */ |
| 1721 | + retu_write_reg(rtc->dev, RETU_REG_RTCCALR, 0x00c0); |
| 1722 | + |
| 1723 | + rtc->alarm_expired = 0; |
| 1724 | +} |
| 1725 | + |
| 1726 | +static irqreturn_t retu_rtc_interrupt(int irq, void *_rtc) |
| 1727 | +{ |
| 1728 | + struct retu_rtc *rtc = _rtc; |
| 1729 | + |
| 1730 | + mutex_lock(&rtc->mutex); |
| 1731 | + rtc->alarm_expired = 1; |
| 1732 | + retu_write_reg(rtc->dev, RETU_REG_RTCHMAR, (24 << 8) | 60); |
| 1733 | + mutex_unlock(&rtc->mutex); |
| 1734 | + |
| 1735 | + return IRQ_HANDLED; |
| 1736 | +} |
| 1737 | + |
| 1738 | +static int retu_rtc_init_irq(struct retu_rtc *rtc) |
| 1739 | +{ |
| 1740 | + int irq; |
| 1741 | + int ret; |
| 1742 | + |
| 1743 | + irq = platform_get_irq(to_platform_device(rtc->dev), 0); |
| 1744 | + rtc->irq_rtcs = irq; |
| 1745 | + |
| 1746 | + irq = platform_get_irq(to_platform_device(rtc->dev), 1); |
| 1747 | + rtc->irq_rtca = irq; |
| 1748 | + |
| 1749 | + ret = request_threaded_irq(rtc->irq_rtcs, NULL, retu_rtc_interrupt, |
| 1750 | + 0, "RTCS", rtc); |
| 1751 | + if (ret != 0) |
| 1752 | + return ret; |
| 1753 | + |
| 1754 | + ret = request_threaded_irq(rtc->irq_rtca, NULL, retu_rtc_interrupt, |
| 1755 | + 0, "RTCA", rtc); |
| 1756 | + if (ret != 0) { |
| 1757 | + free_irq(rtc->irq_rtcs, rtc); |
| 1758 | + return ret; |
| 1759 | + } |
| 1760 | + |
| 1761 | + return 0; |
| 1762 | +} |
| 1763 | + |
| 1764 | +static int retu_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alm) |
| 1765 | +{ |
| 1766 | + struct retu_rtc *rtc = dev_get_drvdata(dev); |
| 1767 | + u16 chmar; |
| 1768 | + |
| 1769 | + mutex_lock(&rtc->mutex); |
| 1770 | + |
| 1771 | + chmar = ((alm->time.tm_hour & 0x1f) << 8) | (alm->time.tm_min & 0x3f); |
| 1772 | + retu_write_reg(rtc->dev, RETU_REG_RTCHMAR, chmar); |
| 1773 | + |
| 1774 | + mutex_unlock(&rtc->mutex); |
| 1775 | + |
| 1776 | + return 0; |
| 1777 | +} |
| 1778 | + |
| 1779 | +static int retu_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alm) |
| 1780 | +{ |
| 1781 | + struct retu_rtc *rtc = dev_get_drvdata(dev); |
| 1782 | + u16 chmar; |
| 1783 | + |
| 1784 | + mutex_lock(&rtc->mutex); |
| 1785 | + |
| 1786 | + chmar = retu_read_reg(rtc->dev, RETU_REG_RTCHMAR); |
| 1787 | + |
| 1788 | + alm->time.tm_hour = (chmar >> 8) & 0x1f; |
| 1789 | + alm->time.tm_min = chmar & 0x3f; |
| 1790 | + alm->enabled = !!rtc->alarm_expired; |
| 1791 | + |
| 1792 | + mutex_unlock(&rtc->mutex); |
| 1793 | + |
| 1794 | + return 0; |
| 1795 | +} |
| 1796 | + |
| 1797 | +static int retu_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 1798 | +{ |
| 1799 | + struct retu_rtc *rtc = dev_get_drvdata(dev); |
| 1800 | + u16 dsr; |
| 1801 | + u16 hmr; |
| 1802 | + |
| 1803 | + dsr = ((tm->tm_mday & 0xff) << 8) | (tm->tm_hour & 0xff); |
| 1804 | + hmr = ((tm->tm_min & 0xff) << 8) | (tm->tm_sec & 0xff); |
| 1805 | + |
| 1806 | + mutex_lock(&rtc->mutex); |
| 1807 | + |
| 1808 | + retu_write_reg(rtc->dev, RETU_REG_RTCDSR, dsr); |
| 1809 | + retu_write_reg(rtc->dev, RETU_REG_RTCHMR, hmr); |
| 1810 | + |
| 1811 | + mutex_unlock(&rtc->mutex); |
| 1812 | + |
| 1813 | + return 0; |
| 1814 | +} |
| 1815 | + |
| 1816 | +static int retu_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 1817 | +{ |
| 1818 | + struct retu_rtc *rtc = dev_get_drvdata(dev); |
| 1819 | + u16 dsr; |
| 1820 | + u16 hmr; |
| 1821 | + |
| 1822 | + /* |
| 1823 | + * DSR holds days and hours |
| 1824 | + * HMR hols minutes and seconds |
| 1825 | + * |
| 1826 | + * both are 16 bit registers with 8-bit for each field. |
| 1827 | + */ |
| 1828 | + |
| 1829 | + mutex_lock(&rtc->mutex); |
| 1830 | + |
| 1831 | + dsr = retu_read_reg(rtc->dev, RETU_REG_RTCDSR); |
| 1832 | + hmr = retu_read_reg(rtc->dev, RETU_REG_RTCHMR); |
| 1833 | + |
| 1834 | + tm->tm_sec = hmr & 0xff; |
| 1835 | + tm->tm_min = hmr >> 8; |
| 1836 | + tm->tm_hour = dsr & 0xff; |
| 1837 | + tm->tm_mday = dsr >> 8; |
| 1838 | + |
| 1839 | + mutex_unlock(&rtc->mutex); |
| 1840 | + |
| 1841 | + return 0; |
| 1842 | +} |
| 1843 | + |
| 1844 | +static struct rtc_class_ops retu_rtc_ops = { |
| 1845 | + .read_time = retu_rtc_read_time, |
| 1846 | + .set_time = retu_rtc_set_time, |
| 1847 | + .read_alarm = retu_rtc_read_alarm, |
| 1848 | + .set_alarm = retu_rtc_set_alarm, |
| 1849 | +}; |
| 1850 | + |
| 1851 | +static int __init retu_rtc_probe(struct platform_device *pdev) |
| 1852 | +{ |
| 1853 | + struct retu_rtc *rtc; |
| 1854 | + int r; |
| 1855 | + |
| 1856 | + rtc = kzalloc(sizeof(*rtc), GFP_KERNEL); |
| 1857 | + if (!rtc) { |
| 1858 | + dev_err(&pdev->dev, "not enough memory\n"); |
| 1859 | + r = -ENOMEM; |
| 1860 | + goto err0; |
| 1861 | + } |
| 1862 | + |
| 1863 | + rtc->dev = &pdev->dev; |
| 1864 | + platform_set_drvdata(pdev, rtc); |
| 1865 | + mutex_init(&rtc->mutex); |
| 1866 | + |
| 1867 | + rtc->alarm_expired = retu_read_reg(rtc->dev, RETU_REG_IDR) & |
| 1868 | + (0x1 << RETU_INT_RTCA); |
| 1869 | + |
| 1870 | + r = retu_rtc_init_irq(rtc); |
| 1871 | + if (r < 0) { |
| 1872 | + dev_err(&pdev->dev, "failed to request retu irq\n"); |
| 1873 | + goto err1; |
| 1874 | + } |
| 1875 | + |
| 1876 | + /* If the calibration register is zero, we've probably lost power */ |
| 1877 | + if (!(retu_read_reg(rtc->dev, RETU_REG_RTCCALR) & 0x00ff)) |
| 1878 | + retu_rtc_do_reset(rtc); |
| 1879 | + |
| 1880 | + rtc->rtc = rtc_device_register(pdev->name, &pdev->dev, & |
| 1881 | + retu_rtc_ops, THIS_MODULE); |
| 1882 | + if (IS_ERR(rtc->rtc)) { |
| 1883 | + dev_err(&pdev->dev, "can't register RTC device\n"); |
| 1884 | + goto err2; |
| 1885 | + } |
| 1886 | + |
| 1887 | + return 0; |
| 1888 | + |
| 1889 | +err2: |
| 1890 | + free_irq(rtc->irq_rtcs, rtc); |
| 1891 | + free_irq(rtc->irq_rtca, rtc); |
| 1892 | + |
| 1893 | +err1: |
| 1894 | + kfree(rtc); |
| 1895 | + |
| 1896 | +err0: |
| 1897 | + return r; |
| 1898 | +} |
| 1899 | + |
| 1900 | +static int __devexit retu_rtc_remove(struct platform_device *pdev) |
| 1901 | +{ |
| 1902 | + struct retu_rtc *rtc = platform_get_drvdata(pdev); |
| 1903 | + |
| 1904 | + free_irq(rtc->irq_rtcs, rtc); |
| 1905 | + free_irq(rtc->irq_rtca, rtc); |
| 1906 | + rtc_device_unregister(rtc->rtc); |
| 1907 | + kfree(rtc); |
| 1908 | + |
| 1909 | + return 0; |
| 1910 | +} |
| 1911 | + |
| 1912 | +static struct platform_driver retu_rtc_driver = { |
| 1913 | + .remove = __exit_p(retu_rtc_remove), |
| 1914 | + .driver = { |
| 1915 | + .name = "retu-rtc", |
| 1916 | + }, |
| 1917 | +}; |
| 1918 | + |
| 1919 | +static int __init retu_rtc_init(void) |
| 1920 | +{ |
| 1921 | + return platform_driver_probe(&retu_rtc_driver, retu_rtc_probe); |
| 1922 | +} |
| 1923 | +module_init(retu_rtc_init); |
| 1924 | + |
| 1925 | +static void __exit retu_rtc_exit(void) |
| 1926 | +{ |
| 1927 | + platform_driver_unregister(&retu_rtc_driver); |
| 1928 | +} |
| 1929 | +module_exit(retu_rtc_exit); |
| 1930 | + |
| 1931 | +MODULE_DESCRIPTION("Retu RTC"); |
| 1932 | +MODULE_LICENSE("GPL"); |
| 1933 | +MODULE_AUTHOR("Paul Mundt"); |
| 1934 | +MODULE_AUTHOR("Igor Stoppa"); |
| 1935 | +MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>"); |
| 1936 | + |
| 1937 | --- /dev/null |
| 1938 | +++ b/drivers/cbus/retu-wdt.c |
| 1939 | @@ -0,0 +1,272 @@ |
| 1940 | +/** |
| 1941 | + * drivers/cbus/retu-wdt.c |
| 1942 | + * |
| 1943 | + * Driver for Retu watchdog |
| 1944 | + * |
| 1945 | + * Copyright (C) 2004, 2005 Nokia Corporation |
| 1946 | + * |
| 1947 | + * Written by Amit Kucheria <amit.kucheria@nokia.com> |
| 1948 | + * |
| 1949 | + * Cleanups by Michael Buesch <mb@bu3sch.de> (C) 2011 |
| 1950 | + * |
| 1951 | + * This file is subject to the terms and conditions of the GNU General |
| 1952 | + * Public License. See the file "COPYING" in the main directory of this |
| 1953 | + * archive for more details. |
| 1954 | + * |
| 1955 | + * This program is distributed in the hope that it will be useful, |
| 1956 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 1957 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 1958 | + * GNU General Public License for more details. |
| 1959 | + * |
| 1960 | + * You should have received a copy of the GNU General Public License |
| 1961 | + * along with this program; if not, write to the Free Software |
| 1962 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 1963 | + */ |
| 1964 | + |
| 1965 | +#include <linux/kernel.h> |
| 1966 | +#include <linux/slab.h> |
| 1967 | +#include <linux/module.h> |
| 1968 | +#include <linux/device.h> |
| 1969 | +#include <linux/init.h> |
| 1970 | +#include <linux/fs.h> |
| 1971 | +#include <linux/io.h> |
| 1972 | +#include <linux/platform_device.h> |
| 1973 | + |
| 1974 | +#include <linux/completion.h> |
| 1975 | +#include <linux/errno.h> |
| 1976 | +#include <linux/moduleparam.h> |
| 1977 | +#include <linux/miscdevice.h> |
| 1978 | +#include <linux/watchdog.h> |
| 1979 | + |
| 1980 | +#include <asm/uaccess.h> |
| 1981 | + |
| 1982 | +#include <plat/prcm.h> |
| 1983 | + |
| 1984 | +#include "cbus.h" |
| 1985 | +#include "retu.h" |
| 1986 | + |
| 1987 | +/* Watchdog timeout in seconds */ |
| 1988 | +#define RETU_WDT_MIN_TIMER 0 |
| 1989 | +#define RETU_WDT_DEFAULT_TIMER 32 |
| 1990 | +#define RETU_WDT_MAX_TIMER 63 |
| 1991 | + |
| 1992 | +struct retu_wdt_dev { |
| 1993 | + struct device *dev; |
| 1994 | + unsigned int period_val; /* Current period of watchdog */ |
| 1995 | + unsigned long users; |
| 1996 | + struct miscdevice miscdev; |
| 1997 | + struct delayed_work ping_work; |
| 1998 | + struct mutex mutex; |
| 1999 | +}; |
| 2000 | + |
| 2001 | + |
| 2002 | +static inline void _retu_modify_counter(struct retu_wdt_dev *wdev, |
| 2003 | + unsigned int new) |
| 2004 | +{ |
| 2005 | + retu_write_reg(wdev->dev, RETU_REG_WATCHDOG, (u16)new); |
| 2006 | +} |
| 2007 | + |
| 2008 | +static int retu_modify_counter(struct retu_wdt_dev *wdev, unsigned int new) |
| 2009 | +{ |
| 2010 | + if (new < RETU_WDT_MIN_TIMER || new > RETU_WDT_MAX_TIMER) |
| 2011 | + return -EINVAL; |
| 2012 | + |
| 2013 | + mutex_lock(&wdev->mutex); |
| 2014 | + wdev->period_val = new; |
| 2015 | + _retu_modify_counter(wdev, wdev->period_val); |
| 2016 | + mutex_unlock(&wdev->mutex); |
| 2017 | + |
| 2018 | + return 0; |
| 2019 | +} |
| 2020 | + |
| 2021 | +/* |
| 2022 | + * Since retu watchdog cannot be disabled in hardware, we must kick it |
| 2023 | + * with a timer until userspace watchdog software takes over. Do this |
| 2024 | + * unless /dev/watchdog is open or CONFIG_WATCHDOG_NOWAYOUT is set. |
| 2025 | + */ |
| 2026 | +static void retu_wdt_ping_enable(struct retu_wdt_dev *wdev) |
| 2027 | +{ |
| 2028 | + _retu_modify_counter(wdev, RETU_WDT_MAX_TIMER); |
| 2029 | + schedule_delayed_work(&wdev->ping_work, |
| 2030 | + round_jiffies_relative(RETU_WDT_DEFAULT_TIMER * HZ)); |
| 2031 | +} |
| 2032 | + |
| 2033 | +static void retu_wdt_ping_disable(struct retu_wdt_dev *wdev) |
| 2034 | +{ |
| 2035 | + _retu_modify_counter(wdev, RETU_WDT_MAX_TIMER); |
| 2036 | + cancel_delayed_work_sync(&wdev->ping_work); |
| 2037 | +} |
| 2038 | + |
| 2039 | +static void retu_wdt_ping_work(struct work_struct *work) |
| 2040 | +{ |
| 2041 | + struct retu_wdt_dev *wdev = container_of(to_delayed_work(work), |
| 2042 | + struct retu_wdt_dev, ping_work); |
| 2043 | + retu_wdt_ping_enable(wdev); |
| 2044 | +} |
| 2045 | + |
| 2046 | +static int retu_wdt_open(struct inode *inode, struct file *file) |
| 2047 | +{ |
| 2048 | + struct miscdevice *mdev = file->private_data; |
| 2049 | + struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev); |
| 2050 | + |
| 2051 | + if (test_and_set_bit(0, &wdev->users)) |
| 2052 | + return -EBUSY; |
| 2053 | + |
| 2054 | + retu_wdt_ping_disable(wdev); |
| 2055 | + |
| 2056 | + return nonseekable_open(inode, file); |
| 2057 | +} |
| 2058 | + |
| 2059 | +static int retu_wdt_release(struct inode *inode, struct file *file) |
| 2060 | +{ |
| 2061 | + struct miscdevice *mdev = file->private_data; |
| 2062 | + struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev); |
| 2063 | + |
| 2064 | +#ifndef CONFIG_WATCHDOG_NOWAYOUT |
| 2065 | + retu_wdt_ping_enable(wdev); |
| 2066 | +#endif |
| 2067 | + clear_bit(0, &wdev->users); |
| 2068 | + |
| 2069 | + return 0; |
| 2070 | +} |
| 2071 | + |
| 2072 | +static ssize_t retu_wdt_write(struct file *file, const char __user *data, |
| 2073 | + size_t len, loff_t *ppos) |
| 2074 | +{ |
| 2075 | + struct miscdevice *mdev = file->private_data; |
| 2076 | + struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev); |
| 2077 | + |
| 2078 | + if (len) |
| 2079 | + retu_modify_counter(wdev, RETU_WDT_MAX_TIMER); |
| 2080 | + |
| 2081 | + return len; |
| 2082 | +} |
| 2083 | + |
| 2084 | +static long retu_wdt_ioctl(struct file *file, unsigned int cmd, |
| 2085 | + unsigned long arg) |
| 2086 | +{ |
| 2087 | + struct miscdevice *mdev = file->private_data; |
| 2088 | + struct retu_wdt_dev *wdev = container_of(mdev, struct retu_wdt_dev, miscdev); |
| 2089 | + int new_margin; |
| 2090 | + |
| 2091 | + static const struct watchdog_info ident = { |
| 2092 | + .identity = "Retu Watchdog", |
| 2093 | + .options = WDIOF_SETTIMEOUT, |
| 2094 | + .firmware_version = 0, |
| 2095 | + }; |
| 2096 | + |
| 2097 | + switch (cmd) { |
| 2098 | + default: |
| 2099 | + return -ENOTTY; |
| 2100 | + case WDIOC_GETSUPPORT: |
| 2101 | + return copy_to_user((struct watchdog_info __user *)arg, &ident, |
| 2102 | + sizeof(ident)); |
| 2103 | + case WDIOC_GETSTATUS: |
| 2104 | + return put_user(0, (int __user *)arg); |
| 2105 | + case WDIOC_GETBOOTSTATUS: |
| 2106 | + if (cpu_is_omap16xx()) |
| 2107 | + return put_user(omap_readw(ARM_SYSST), |
| 2108 | + (int __user *)arg); |
| 2109 | + if (cpu_is_omap24xx()) |
| 2110 | + return put_user(omap_prcm_get_reset_sources(), |
| 2111 | + (int __user *)arg); |
| 2112 | + case WDIOC_KEEPALIVE: |
| 2113 | + retu_modify_counter(wdev, RETU_WDT_MAX_TIMER); |
| 2114 | + break; |
| 2115 | + case WDIOC_SETTIMEOUT: |
| 2116 | + if (get_user(new_margin, (int __user *)arg)) |
| 2117 | + return -EFAULT; |
| 2118 | + retu_modify_counter(wdev, new_margin); |
| 2119 | + /* Fall through */ |
| 2120 | + case WDIOC_GETTIMEOUT: |
| 2121 | + return put_user(wdev->period_val, (int __user *)arg); |
| 2122 | + } |
| 2123 | + |
| 2124 | + return 0; |
| 2125 | +} |
| 2126 | + |
| 2127 | +static const struct file_operations retu_wdt_fops = { |
| 2128 | + .owner = THIS_MODULE, |
| 2129 | + .write = retu_wdt_write, |
| 2130 | + .unlocked_ioctl = retu_wdt_ioctl, |
| 2131 | + .open = retu_wdt_open, |
| 2132 | + .release = retu_wdt_release, |
| 2133 | +}; |
| 2134 | + |
| 2135 | +static int __init retu_wdt_probe(struct platform_device *pdev) |
| 2136 | +{ |
| 2137 | + struct retu_wdt_dev *wdev; |
| 2138 | + int ret; |
| 2139 | + |
| 2140 | + wdev = kzalloc(sizeof(struct retu_wdt_dev), GFP_KERNEL); |
| 2141 | + if (!wdev) |
| 2142 | + return -ENOMEM; |
| 2143 | + |
| 2144 | + wdev->dev = &pdev->dev; |
| 2145 | + wdev->period_val = RETU_WDT_DEFAULT_TIMER; |
| 2146 | + mutex_init(&wdev->mutex); |
| 2147 | + |
| 2148 | + platform_set_drvdata(pdev, wdev); |
| 2149 | + |
| 2150 | + wdev->miscdev.parent = &pdev->dev; |
| 2151 | + wdev->miscdev.minor = WATCHDOG_MINOR; |
| 2152 | + wdev->miscdev.name = "watchdog"; |
| 2153 | + wdev->miscdev.fops = &retu_wdt_fops; |
| 2154 | + |
| 2155 | + ret = misc_register(&wdev->miscdev); |
| 2156 | + if (ret) |
| 2157 | + goto err_free_wdev; |
| 2158 | + |
| 2159 | + INIT_DELAYED_WORK(&wdev->ping_work, retu_wdt_ping_work); |
| 2160 | + |
| 2161 | + /* Kick the watchdog for kernel booting to finish. |
| 2162 | + * If nowayout is not set, we start the ping work. */ |
| 2163 | +#ifdef CONFIG_WATCHDOG_NOWAYOUT |
| 2164 | + retu_modify_counter(wdev, RETU_WDT_MAX_TIMER); |
| 2165 | +#else |
| 2166 | + retu_wdt_ping_enable(wdev); |
| 2167 | +#endif |
| 2168 | + |
| 2169 | + return 0; |
| 2170 | + |
| 2171 | +err_free_wdev: |
| 2172 | + kfree(wdev); |
| 2173 | + |
| 2174 | + return ret; |
| 2175 | +} |
| 2176 | + |
| 2177 | +static int __devexit retu_wdt_remove(struct platform_device *pdev) |
| 2178 | +{ |
| 2179 | + struct retu_wdt_dev *wdev; |
| 2180 | + |
| 2181 | + wdev = platform_get_drvdata(pdev); |
| 2182 | + misc_deregister(&wdev->miscdev); |
| 2183 | + cancel_delayed_work_sync(&wdev->ping_work); |
| 2184 | + kfree(wdev); |
| 2185 | + |
| 2186 | + return 0; |
| 2187 | +} |
| 2188 | + |
| 2189 | +static struct platform_driver retu_wdt_driver = { |
| 2190 | + .remove = __exit_p(retu_wdt_remove), |
| 2191 | + .driver = { |
| 2192 | + .name = "retu-wdt", |
| 2193 | + }, |
| 2194 | +}; |
| 2195 | + |
| 2196 | +static int __init retu_wdt_init(void) |
| 2197 | +{ |
| 2198 | + return platform_driver_probe(&retu_wdt_driver, retu_wdt_probe); |
| 2199 | +} |
| 2200 | + |
| 2201 | +static void __exit retu_wdt_exit(void) |
| 2202 | +{ |
| 2203 | + platform_driver_unregister(&retu_wdt_driver); |
| 2204 | +} |
| 2205 | + |
| 2206 | +module_init(retu_wdt_init); |
| 2207 | +module_exit(retu_wdt_exit); |
| 2208 | + |
| 2209 | +MODULE_DESCRIPTION("Retu WatchDog"); |
| 2210 | +MODULE_AUTHOR("Amit Kucheria"); |
| 2211 | +MODULE_LICENSE("GPL"); |
| 2212 | --- /dev/null |
| 2213 | +++ b/drivers/cbus/tahvo.c |
| 2214 | @@ -0,0 +1,423 @@ |
| 2215 | +/** |
| 2216 | + * drivers/cbus/tahvo.c |
| 2217 | + * |
| 2218 | + * Support functions for Tahvo ASIC |
| 2219 | + * |
| 2220 | + * Copyright (C) 2004, 2005 Nokia Corporation |
| 2221 | + * |
| 2222 | + * Written by Juha Yrjölä <juha.yrjola@nokia.com>, |
| 2223 | + * David Weinehall <david.weinehall@nokia.com>, and |
| 2224 | + * Mikko Ylinen <mikko.k.ylinen@nokia.com> |
| 2225 | + * |
| 2226 | + * This file is subject to the terms and conditions of the GNU General |
| 2227 | + * Public License. See the file "COPYING" in the main directory of this |
| 2228 | + * archive for more details. |
| 2229 | + * |
| 2230 | + * This program is distributed in the hope that it will be useful, |
| 2231 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 2232 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 2233 | + * GNU General Public License for more details. |
| 2234 | + * |
| 2235 | + * You should have received a copy of the GNU General Public License |
| 2236 | + * along with this program; if not, write to the Free Software |
| 2237 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 2238 | + */ |
| 2239 | + |
| 2240 | +#include <linux/module.h> |
| 2241 | +#include <linux/init.h> |
| 2242 | + |
| 2243 | +#include <linux/slab.h> |
| 2244 | +#include <linux/kernel.h> |
| 2245 | +#include <linux/errno.h> |
| 2246 | +#include <linux/device.h> |
| 2247 | +#include <linux/irq.h> |
| 2248 | +#include <linux/interrupt.h> |
| 2249 | +#include <linux/platform_device.h> |
| 2250 | +#include <linux/platform_data/cbus.h> |
| 2251 | +#include <linux/mutex.h> |
| 2252 | + |
| 2253 | +#include "cbus.h" |
| 2254 | +#include "tahvo.h" |
| 2255 | + |
| 2256 | +struct tahvo { |
| 2257 | + /* device lock */ |
| 2258 | + struct mutex mutex; |
| 2259 | + struct device *dev; |
| 2260 | + |
| 2261 | + int irq_base; |
| 2262 | + int irq_end; |
| 2263 | + int irq; |
| 2264 | + |
| 2265 | + int ack; |
| 2266 | + int mask; |
| 2267 | + |
| 2268 | + unsigned int mask_pending:1; |
| 2269 | + unsigned int ack_pending:1; |
| 2270 | + unsigned int is_betty:1; |
| 2271 | +}; |
| 2272 | + |
| 2273 | +/** |
| 2274 | + * __tahvo_read_reg - Reads a value from a register in Tahvo |
| 2275 | + * @tahvo: pointer to tahvo structure |
| 2276 | + * @reg: the register address to read from |
| 2277 | + */ |
| 2278 | +static int __tahvo_read_reg(struct tahvo *tahvo, unsigned reg) |
| 2279 | +{ |
| 2280 | + return cbus_read_reg(tahvo->dev, CBUS_TAHVO_DEVICE_ID, reg); |
| 2281 | +} |
| 2282 | + |
| 2283 | +/** |
| 2284 | + * __tahvo_write_reg - Writes a value to a register in Tahvo |
| 2285 | + * @tahvo: pointer to tahvo structure |
| 2286 | + * @reg: register address to write to |
| 2287 | + * @val: the value to be written to @reg |
| 2288 | + */ |
| 2289 | +static void __tahvo_write_reg(struct tahvo *tahvo, unsigned reg, u16 val) |
| 2290 | +{ |
| 2291 | + cbus_write_reg(tahvo->dev, CBUS_TAHVO_DEVICE_ID, reg, val); |
| 2292 | +} |
| 2293 | + |
| 2294 | +/** |
| 2295 | + * tahvo_read_reg - Read a value from a register in Tahvo |
| 2296 | + * @child: device pointer from the calling child |
| 2297 | + * @reg: the register to read from |
| 2298 | + * |
| 2299 | + * This function returns the contents of the specified register |
| 2300 | + */ |
| 2301 | +int tahvo_read_reg(struct device *child, unsigned reg) |
| 2302 | +{ |
| 2303 | + struct tahvo *tahvo = dev_get_drvdata(child->parent); |
| 2304 | + |
| 2305 | + return __tahvo_read_reg(tahvo, reg); |
| 2306 | +} |
| 2307 | +EXPORT_SYMBOL(tahvo_read_reg); |
| 2308 | + |
| 2309 | +/** |
| 2310 | + * tahvo_write_reg - Write a value to a register in Tahvo |
| 2311 | + * @child: device pointer from the calling child |
| 2312 | + * @reg: the register to write to |
| 2313 | + * @val : the value to write to the register |
| 2314 | + * |
| 2315 | + * This function writes a value to the specified register |
| 2316 | + */ |
| 2317 | +void tahvo_write_reg(struct device *child, unsigned reg, u16 val) |
| 2318 | +{ |
| 2319 | + struct tahvo *tahvo = dev_get_drvdata(child->parent); |
| 2320 | + |
| 2321 | + __tahvo_write_reg(tahvo, reg, val); |
| 2322 | +} |
| 2323 | +EXPORT_SYMBOL(tahvo_write_reg); |
| 2324 | + |
| 2325 | +/** |
| 2326 | + * tahvo_set_clear_reg_bits - set and clear register bits atomically |
| 2327 | + * @child: device pointer from the calling child |
| 2328 | + * @reg: the register to write to |
| 2329 | + * @bits: the bits to set |
| 2330 | + * |
| 2331 | + * This function sets and clears the specified Tahvo register bits atomically |
| 2332 | + */ |
| 2333 | +void tahvo_set_clear_reg_bits(struct device *child, unsigned reg, u16 set, |
| 2334 | + u16 clear) |
| 2335 | +{ |
| 2336 | + struct tahvo *tahvo = dev_get_drvdata(child->parent); |
| 2337 | + u16 w; |
| 2338 | + |
| 2339 | + mutex_lock(&tahvo->mutex); |
| 2340 | + w = __tahvo_read_reg(tahvo, reg); |
| 2341 | + w &= ~clear; |
| 2342 | + w |= set; |
| 2343 | + __tahvo_write_reg(tahvo, reg, w); |
| 2344 | + mutex_unlock(&tahvo->mutex); |
| 2345 | +} |
| 2346 | + |
| 2347 | +static irqreturn_t tahvo_irq_handler(int irq, void *_tahvo) |
| 2348 | +{ |
| 2349 | + struct tahvo *tahvo = _tahvo; |
| 2350 | + u16 id; |
| 2351 | + u16 im; |
| 2352 | + |
| 2353 | + id = __tahvo_read_reg(tahvo, TAHVO_REG_IDR); |
| 2354 | + im = __tahvo_read_reg(tahvo, TAHVO_REG_IMR); |
| 2355 | + id &= ~im; |
| 2356 | + |
| 2357 | + if (!id) { |
| 2358 | + dev_vdbg(tahvo->dev, "No IRQ, spurious ?\n"); |
| 2359 | + return IRQ_NONE; |
| 2360 | + } |
| 2361 | + |
| 2362 | + while (id) { |
| 2363 | + unsigned long pending = __ffs(id); |
| 2364 | + unsigned int irq; |
| 2365 | + |
| 2366 | + id &= ~BIT(pending); |
| 2367 | + irq = pending + tahvo->irq_base; |
| 2368 | + handle_nested_irq(irq); |
| 2369 | + } |
| 2370 | + |
| 2371 | + return IRQ_HANDLED; |
| 2372 | +} |
| 2373 | + |
| 2374 | +/* -------------------------------------------------------------------------- */ |
| 2375 | + |
| 2376 | +static void tahvo_irq_bus_lock(struct irq_data *data) |
| 2377 | +{ |
| 2378 | + struct tahvo *tahvo = irq_data_get_irq_chip_data(data); |
| 2379 | + |
| 2380 | + mutex_lock(&tahvo->mutex); |
| 2381 | +} |
| 2382 | + |
| 2383 | +static void tahvo_irq_bus_sync_unlock(struct irq_data *data) |
| 2384 | +{ |
| 2385 | + struct tahvo *tahvo = irq_data_get_irq_chip_data(data); |
| 2386 | + |
| 2387 | + if (tahvo->mask_pending) { |
| 2388 | + __tahvo_write_reg(tahvo, TAHVO_REG_IMR, tahvo->mask); |
| 2389 | + tahvo->mask_pending = false; |
| 2390 | + } |
| 2391 | + |
| 2392 | + if (tahvo->ack_pending) { |
| 2393 | + __tahvo_write_reg(tahvo, TAHVO_REG_IDR, tahvo->ack); |
| 2394 | + tahvo->ack_pending = false; |
| 2395 | + } |
| 2396 | + |
| 2397 | + mutex_unlock(&tahvo->mutex); |
| 2398 | +} |
| 2399 | + |
| 2400 | +static void tahvo_irq_mask(struct irq_data *data) |
| 2401 | +{ |
| 2402 | + struct tahvo *tahvo = irq_data_get_irq_chip_data(data); |
| 2403 | + int irq = data->irq; |
| 2404 | + |
| 2405 | + tahvo->mask |= (1 << (irq - tahvo->irq_base)); |
| 2406 | + tahvo->mask_pending = true; |
| 2407 | +} |
| 2408 | + |
| 2409 | +static void tahvo_irq_unmask(struct irq_data *data) |
| 2410 | +{ |
| 2411 | + struct tahvo *tahvo = irq_data_get_irq_chip_data(data); |
| 2412 | + int irq = data->irq; |
| 2413 | + |
| 2414 | + tahvo->mask &= ~(1 << (irq - tahvo->irq_base)); |
| 2415 | + tahvo->mask_pending = true; |
| 2416 | +} |
| 2417 | + |
| 2418 | +static void tahvo_irq_ack(struct irq_data *data) |
| 2419 | +{ |
| 2420 | + struct tahvo *tahvo = irq_data_get_irq_chip_data(data); |
| 2421 | + int irq = data->irq; |
| 2422 | + |
| 2423 | + tahvo->ack |= (1 << (irq - tahvo->irq_base)); |
| 2424 | + tahvo->ack_pending = true; |
| 2425 | +} |
| 2426 | + |
| 2427 | +static struct irq_chip tahvo_irq_chip = { |
| 2428 | + .name = "tahvo", |
| 2429 | + .irq_bus_lock = tahvo_irq_bus_lock, |
| 2430 | + .irq_bus_sync_unlock = tahvo_irq_bus_sync_unlock, |
| 2431 | + .irq_mask = tahvo_irq_mask, |
| 2432 | + .irq_unmask = tahvo_irq_unmask, |
| 2433 | + .irq_ack = tahvo_irq_ack, |
| 2434 | +}; |
| 2435 | + |
| 2436 | +static inline void tahvo_irq_setup(int irq) |
| 2437 | +{ |
| 2438 | +#ifdef CONFIG_ARM |
| 2439 | + set_irq_flags(irq, IRQF_VALID); |
| 2440 | +#else |
| 2441 | + irq_set_noprobe(irq); |
| 2442 | +#endif |
| 2443 | +} |
| 2444 | + |
| 2445 | +static void tahvo_irq_init(struct tahvo *tahvo) |
| 2446 | +{ |
| 2447 | + int base = tahvo->irq_base; |
| 2448 | + int end = tahvo->irq_end; |
| 2449 | + int irq; |
| 2450 | + |
| 2451 | + for (irq = base; irq < end; irq++) { |
| 2452 | + irq_set_chip_data(irq, tahvo); |
| 2453 | + irq_set_chip_and_handler(irq, &tahvo_irq_chip, |
| 2454 | + handle_simple_irq); |
| 2455 | + irq_set_nested_thread(irq, 1); |
| 2456 | + tahvo_irq_setup(irq); |
| 2457 | + } |
| 2458 | +} |
| 2459 | + |
| 2460 | +/* -------------------------------------------------------------------------- */ |
| 2461 | + |
| 2462 | +static struct resource generic_resources[] = { |
| 2463 | + { |
| 2464 | + .start = -EINVAL, /* fixed later */ |
| 2465 | + .flags = IORESOURCE_IRQ, |
| 2466 | + }, |
| 2467 | +}; |
| 2468 | + |
| 2469 | +static struct device *tahvo_allocate_child(const char *name, |
| 2470 | + struct device *parent, int irq) |
| 2471 | +{ |
| 2472 | + struct platform_device *pdev; |
| 2473 | + int ret; |
| 2474 | + |
| 2475 | + pdev = platform_device_alloc(name, -1); |
| 2476 | + if (!pdev) { |
| 2477 | + dev_dbg(parent, "can't allocate %s\n", name); |
| 2478 | + goto err0; |
| 2479 | + } |
| 2480 | + |
| 2481 | + pdev->dev.parent = parent; |
| 2482 | + |
| 2483 | + if (irq > 0) { |
| 2484 | + generic_resources[0].start = irq; |
| 2485 | + |
| 2486 | + ret = platform_device_add_resources(pdev, generic_resources, |
| 2487 | + ARRAY_SIZE(generic_resources)); |
| 2488 | + if (ret < 0) { |
| 2489 | + dev_dbg(parent, "can't add resources to %s\n", name); |
| 2490 | + goto err1; |
| 2491 | + } |
| 2492 | + } |
| 2493 | + |
| 2494 | + ret = platform_device_add(pdev); |
| 2495 | + if (ret < 0) { |
| 2496 | + dev_dbg(parent, "can't add %s\n", name); |
| 2497 | + goto err1; |
| 2498 | + } |
| 2499 | + |
| 2500 | + return &pdev->dev; |
| 2501 | + |
| 2502 | +err1: |
| 2503 | + platform_device_put(pdev); |
| 2504 | + |
| 2505 | +err0: |
| 2506 | + return NULL; |
| 2507 | +} |
| 2508 | + |
| 2509 | +static int tahvo_allocate_children(struct device *parent, int irq_base) |
| 2510 | +{ |
| 2511 | + struct device *child; |
| 2512 | + |
| 2513 | + child = tahvo_allocate_child("tahvo-usb", parent, |
| 2514 | + irq_base + TAHVO_INT_VBUSON); |
| 2515 | + if (!child) |
| 2516 | + return -ENOMEM; |
| 2517 | + |
| 2518 | + child = tahvo_allocate_child("tahvo-pwm", parent, -1); |
| 2519 | + if (!child) |
| 2520 | + return -ENOMEM; |
| 2521 | + |
| 2522 | + return 0; |
| 2523 | +} |
| 2524 | + |
| 2525 | +static int __devinit tahvo_probe(struct platform_device *pdev) |
| 2526 | +{ |
| 2527 | + struct tahvo *tahvo; |
| 2528 | + int rev; |
| 2529 | + int ret; |
| 2530 | + int irq; |
| 2531 | + int id; |
| 2532 | + |
| 2533 | + tahvo = kzalloc(sizeof(*tahvo), GFP_KERNEL); |
| 2534 | + if (!tahvo) { |
| 2535 | + dev_err(&pdev->dev, "not enough memory\n"); |
| 2536 | + ret = -ENOMEM; |
| 2537 | + goto err0; |
| 2538 | + } |
| 2539 | + |
| 2540 | + irq = platform_get_irq(pdev, 0); |
| 2541 | + platform_set_drvdata(pdev, tahvo); |
| 2542 | + |
| 2543 | + mutex_init(&tahvo->mutex); |
| 2544 | + |
| 2545 | + ret = irq_alloc_descs(-1, 0, MAX_TAHVO_IRQ_HANDLERS, 0); |
| 2546 | + if (ret < 0) { |
| 2547 | + dev_err(&pdev->dev, "failed to allocate IRQ descs\n"); |
| 2548 | + goto err1; |
| 2549 | + } |
| 2550 | + |
| 2551 | + tahvo->irq_base = ret; |
| 2552 | + tahvo->irq_end = ret + MAX_TAHVO_IRQ_HANDLERS; |
| 2553 | + tahvo->dev = &pdev->dev; |
| 2554 | + tahvo->irq = irq; |
| 2555 | + |
| 2556 | + tahvo_irq_init(tahvo); |
| 2557 | + |
| 2558 | + rev = __tahvo_read_reg(tahvo, TAHVO_REG_ASICR); |
| 2559 | + |
| 2560 | + id = (rev >> 8) & 0xff; |
| 2561 | + |
| 2562 | + if (id == 0x0b) |
| 2563 | + tahvo->is_betty = true; |
| 2564 | + |
| 2565 | + ret = tahvo_allocate_children(&pdev->dev, tahvo->irq_base); |
| 2566 | + if (ret < 0) { |
| 2567 | + dev_err(&pdev->dev, "failed to allocate children\n"); |
| 2568 | + goto err2; |
| 2569 | + } |
| 2570 | + |
| 2571 | + dev_err(&pdev->dev, "%s v%d.%d found\n", |
| 2572 | + tahvo->is_betty ? "Betty" : "Tahvo", |
| 2573 | + (rev >> 4) & 0x0f, rev & 0x0f); |
| 2574 | + |
| 2575 | + /* Mask all TAHVO interrupts */ |
| 2576 | + __tahvo_write_reg(tahvo, TAHVO_REG_IMR, 0xffff); |
| 2577 | + |
| 2578 | + ret = request_threaded_irq(irq, NULL, tahvo_irq_handler, |
| 2579 | + IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
| 2580 | + "tahvo", tahvo); |
| 2581 | + if (ret < 0) { |
| 2582 | + dev_err(&pdev->dev, "Unable to register IRQ handler\n"); |
| 2583 | + goto err2; |
| 2584 | + } |
| 2585 | + |
| 2586 | + return 0; |
| 2587 | + |
| 2588 | +err2: |
| 2589 | + irq_free_descs(tahvo->irq_base, MAX_TAHVO_IRQ_HANDLERS); |
| 2590 | + |
| 2591 | +err1: |
| 2592 | + kfree(tahvo); |
| 2593 | + |
| 2594 | +err0: |
| 2595 | + return ret; |
| 2596 | +} |
| 2597 | + |
| 2598 | +static int __devexit tahvo_remove(struct platform_device *pdev) |
| 2599 | +{ |
| 2600 | + struct tahvo *tahvo = platform_get_drvdata(pdev); |
| 2601 | + int irq; |
| 2602 | + |
| 2603 | + irq = platform_get_irq(pdev, 0); |
| 2604 | + |
| 2605 | + free_irq(irq, 0); |
| 2606 | + irq_free_descs(tahvo->irq_base, MAX_TAHVO_IRQ_HANDLERS); |
| 2607 | + kfree(tahvo); |
| 2608 | + |
| 2609 | + return 0; |
| 2610 | +} |
| 2611 | + |
| 2612 | +static struct platform_driver tahvo_driver = { |
| 2613 | + .probe = tahvo_probe, |
| 2614 | + .remove = __devexit_p(tahvo_remove), |
| 2615 | + .driver = { |
| 2616 | + .name = "tahvo", |
| 2617 | + }, |
| 2618 | +}; |
| 2619 | + |
| 2620 | +static int __init tahvo_init(void) |
| 2621 | +{ |
| 2622 | + return platform_driver_register(&tahvo_driver); |
| 2623 | +} |
| 2624 | +subsys_initcall(tahvo_init); |
| 2625 | + |
| 2626 | +static void __exit tahvo_exit(void) |
| 2627 | +{ |
| 2628 | + platform_driver_unregister(&tahvo_driver); |
| 2629 | +} |
| 2630 | +module_exit(tahvo_exit); |
| 2631 | + |
| 2632 | +MODULE_DESCRIPTION("Tahvo ASIC control"); |
| 2633 | +MODULE_LICENSE("GPL"); |
| 2634 | +MODULE_AUTHOR("Juha Yrjölä"); |
| 2635 | +MODULE_AUTHOR("David Weinehall"); |
| 2636 | +MODULE_AUTHOR("Mikko Ylinen"); |
| 2637 | + |
| 2638 | --- /dev/null |
| 2639 | +++ b/drivers/cbus/tahvo.h |
| 2640 | @@ -0,0 +1,58 @@ |
| 2641 | +/* |
| 2642 | + * drivers/cbus/tahvo.h |
| 2643 | + * |
| 2644 | + * Copyright (C) 2004, 2005 Nokia Corporation |
| 2645 | + * |
| 2646 | + * Written by Juha Yrjölä <juha.yrjola@nokia.com> and |
| 2647 | + * David Weinehall <david.weinehall@nokia.com> |
| 2648 | + * |
| 2649 | + * This file is subject to the terms and conditions of the GNU General |
| 2650 | + * Public License. See the file "COPYING" in the main directory of this |
| 2651 | + * archive for more details. |
| 2652 | + * |
| 2653 | + * This program is distributed in the hope that it will be useful, |
| 2654 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 2655 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 2656 | + * GNU General Public License for more details. |
| 2657 | + |
| 2658 | + * You should have received a copy of the GNU General Public License |
| 2659 | + * along with this program; if not, write to the Free Software |
| 2660 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 2661 | + */ |
| 2662 | + |
| 2663 | +#ifndef __DRIVERS_CBUS_TAHVO_H |
| 2664 | +#define __DRIVERS_CBUS_TAHVO_H |
| 2665 | + |
| 2666 | +#include <linux/types.h> |
| 2667 | + |
| 2668 | +/* Registers */ |
| 2669 | +#define TAHVO_REG_ASICR 0x00 /* ASIC ID & revision */ |
| 2670 | +#define TAHVO_REG_IDR 0x01 /* Interrupt ID */ |
| 2671 | +#define TAHVO_REG_IDSR 0x02 /* Interrupt status */ |
| 2672 | +#define TAHVO_REG_IMR 0x03 /* Interrupt mask */ |
| 2673 | +#define TAHVO_REG_CHGCURR 0x04 /* Charge current control PWM (8-bit) */ |
| 2674 | +#define TAHVO_REG_LEDPWMR 0x05 /* LED PWM */ |
| 2675 | +#define TAHVO_REG_USBR 0x06 /* USB control */ |
| 2676 | +#define TAHVO_REG_CHGCTL 0x08 /* Charge control register */ |
| 2677 | +#define TAHVO_REG_CHGCTL_EN 0x0001 /* Global charge enable */ |
| 2678 | +#define TAHVO_REG_CHGCTL_PWMOVR 0x0004 /* PWM override. Force charge PWM to 0%/100% duty cycle. */ |
| 2679 | +#define TAHVO_REG_CHGCTL_PWMOVRZERO 0x0008 /* If set, PWM override is 0% (If unset -> 100%) */ |
| 2680 | +#define TAHVO_REG_CHGCTL_CURMEAS 0x0040 /* Enable battery current measurement. */ |
| 2681 | +#define TAHVO_REG_CHGCTL_CURTIMRST 0x0080 /* Current measure timer reset. */ |
| 2682 | +#define TAHVO_REG_BATCURRTIMER 0x0c /* Battery current measure timer (8-bit) */ |
| 2683 | +#define TAHVO_REG_BATCURR 0x0d /* Battery (dis)charge current (signed 16-bit) */ |
| 2684 | + |
| 2685 | +#define TAHVO_REG_MAX 0x0d |
| 2686 | + |
| 2687 | +/* Interrupt sources */ |
| 2688 | +#define TAHVO_INT_VBUSON 0 |
| 2689 | +#define TAHVO_INT_BATCURR 7 /* Battery current measure timer */ |
| 2690 | + |
| 2691 | +#define MAX_TAHVO_IRQ_HANDLERS 8 |
| 2692 | + |
| 2693 | +int tahvo_read_reg(struct device *child, unsigned reg); |
| 2694 | +void tahvo_write_reg(struct device *child, unsigned reg, u16 val); |
| 2695 | +void tahvo_set_clear_reg_bits(struct device *child, unsigned reg, u16 set, |
| 2696 | + u16 clear); |
| 2697 | + |
| 2698 | +#endif /* __DRIVERS_CBUS_TAHVO_H */ |
| 2699 | --- /dev/null |
| 2700 | +++ b/drivers/cbus/tahvo-usb.c |
| 2701 | @@ -0,0 +1,740 @@ |
| 2702 | +/** |
| 2703 | + * drivers/cbus/tahvo-usb.c |
| 2704 | + * |
| 2705 | + * Tahvo USB transeiver |
| 2706 | + * |
| 2707 | + * Copyright (C) 2005-2006 Nokia Corporation |
| 2708 | + * |
| 2709 | + * Parts copied from drivers/i2c/chips/isp1301_omap.c |
| 2710 | + * Copyright (C) 2004 Texas Instruments |
| 2711 | + * Copyright (C) 2004 David Brownell |
| 2712 | + * |
| 2713 | + * Written by Juha Yrjölä <juha.yrjola@nokia.com>, |
| 2714 | + * Tony Lindgren <tony@atomide.com>, and |
| 2715 | + * Timo Teräs <timo.teras@nokia.com> |
| 2716 | + * |
| 2717 | + * This file is subject to the terms and conditions of the GNU General |
| 2718 | + * Public License. See the file "COPYING" in the main directory of this |
| 2719 | + * archive for more details. |
| 2720 | + * |
| 2721 | + * This program is distributed in the hope that it will be useful, |
| 2722 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 2723 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 2724 | + * GNU General Public License for more details. |
| 2725 | + * |
| 2726 | + * You should have received a copy of the GNU General Public License |
| 2727 | + * along with this program; if not, write to the Free Software |
| 2728 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 2729 | + */ |
| 2730 | + |
| 2731 | +#include <linux/kernel.h> |
| 2732 | +#include <linux/module.h> |
| 2733 | +#include <linux/init.h> |
| 2734 | +#include <linux/slab.h> |
| 2735 | +#include <linux/io.h> |
| 2736 | +#include <linux/interrupt.h> |
| 2737 | +#include <linux/platform_device.h> |
| 2738 | +#include <linux/usb/ch9.h> |
| 2739 | +#include <linux/usb/gadget.h> |
| 2740 | +#include <linux/usb.h> |
| 2741 | +#include <linux/usb/otg.h> |
| 2742 | +#include <linux/i2c.h> |
| 2743 | +#include <linux/workqueue.h> |
| 2744 | +#include <linux/kobject.h> |
| 2745 | +#include <linux/clk.h> |
| 2746 | +#include <linux/mutex.h> |
| 2747 | + |
| 2748 | +#include <asm/irq.h> |
| 2749 | +#include <plat/usb.h> |
| 2750 | + |
| 2751 | +#include "cbus.h" |
| 2752 | +#include "tahvo.h" |
| 2753 | + |
| 2754 | +#define DRIVER_NAME "tahvo-usb" |
| 2755 | + |
| 2756 | +#define USBR_SLAVE_CONTROL (1 << 8) |
| 2757 | +#define USBR_VPPVIO_SW (1 << 7) |
| 2758 | +#define USBR_SPEED (1 << 6) |
| 2759 | +#define USBR_REGOUT (1 << 5) |
| 2760 | +#define USBR_MASTER_SW2 (1 << 4) |
| 2761 | +#define USBR_MASTER_SW1 (1 << 3) |
| 2762 | +#define USBR_SLAVE_SW (1 << 2) |
| 2763 | +#define USBR_NSUSPEND (1 << 1) |
| 2764 | +#define USBR_SEMODE (1 << 0) |
| 2765 | + |
| 2766 | +/* bits in OTG_CTRL */ |
| 2767 | + |
| 2768 | +/* Bits that are controlled by OMAP OTG and are read-only */ |
| 2769 | +#define OTG_CTRL_OMAP_MASK (OTG_PULLDOWN|OTG_PULLUP|OTG_DRV_VBUS|\ |
| 2770 | + OTG_PD_VBUS|OTG_PU_VBUS|OTG_PU_ID) |
| 2771 | +/* Bits that are controlled by transceiver */ |
| 2772 | +#define OTG_CTRL_XCVR_MASK (OTG_ASESSVLD|OTG_BSESSEND|\ |
| 2773 | + OTG_BSESSVLD|OTG_VBUSVLD|OTG_ID) |
| 2774 | +/* Bits that are controlled by system */ |
| 2775 | +#define OTG_CTRL_SYS_MASK (OTG_A_BUSREQ|OTG_A_SETB_HNPEN|OTG_B_BUSREQ|\ |
| 2776 | + OTG_B_HNPEN|OTG_BUSDROP) |
| 2777 | + |
| 2778 | +#if defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OTG) |
| 2779 | +#error tahvo-otg.c does not work with OCHI yet! |
| 2780 | +#endif |
| 2781 | + |
| 2782 | +#define TAHVO_MODE_HOST 0 |
| 2783 | +#define TAHVO_MODE_PERIPHERAL 1 |
| 2784 | + |
| 2785 | +#ifdef CONFIG_USB_OTG |
| 2786 | +#define TAHVO_MODE(tu) (tu)->tahvo_mode |
| 2787 | +#elif defined(CONFIG_USB_GADGET_OMAP) |
| 2788 | +#define TAHVO_MODE(tu) TAHVO_MODE_PERIPHERAL |
| 2789 | +#else |
| 2790 | +#define TAHVO_MODE(tu) TAHVO_MODE_HOST |
| 2791 | +#endif |
| 2792 | + |
| 2793 | +struct tahvo_usb { |
| 2794 | + struct device *dev; |
| 2795 | + struct platform_device *pt_dev; |
| 2796 | + struct otg_transceiver otg; |
| 2797 | + int vbus_state; |
| 2798 | + struct mutex serialize; |
| 2799 | +#ifdef CONFIG_USB_OTG |
| 2800 | + int tahvo_mode; |
| 2801 | +#endif |
| 2802 | + struct clk *ick; |
| 2803 | + |
| 2804 | + int irq; |
| 2805 | +}; |
| 2806 | +static struct tahvo_usb *tahvo_usb_device; |
| 2807 | + |
| 2808 | +/* |
| 2809 | + * --------------------------------------------------------------------------- |
| 2810 | + * OTG related functions |
| 2811 | + * |
| 2812 | + * These shoud be separated into omap-otg.c driver module, as they are used |
| 2813 | + * by various transceivers. These functions are needed in the UDC-only case |
| 2814 | + * as well. These functions are copied from GPL isp1301_omap.c |
| 2815 | + * --------------------------------------------------------------------------- |
| 2816 | + */ |
| 2817 | +static struct platform_device *tahvo_otg_dev; |
| 2818 | + |
| 2819 | +static irqreturn_t omap_otg_irq(int irq, void *arg) |
| 2820 | +{ |
| 2821 | + u16 otg_irq; |
| 2822 | + |
| 2823 | + otg_irq = omap_readw(OTG_IRQ_SRC); |
| 2824 | + if (otg_irq & OPRT_CHG) { |
| 2825 | + omap_writew(OPRT_CHG, OTG_IRQ_SRC); |
| 2826 | + } else if (otg_irq & B_SRP_TMROUT) { |
| 2827 | + omap_writew(B_SRP_TMROUT, OTG_IRQ_SRC); |
| 2828 | + } else if (otg_irq & B_HNP_FAIL) { |
| 2829 | + omap_writew(B_HNP_FAIL, OTG_IRQ_SRC); |
| 2830 | + } else if (otg_irq & A_SRP_DETECT) { |
| 2831 | + omap_writew(A_SRP_DETECT, OTG_IRQ_SRC); |
| 2832 | + } else if (otg_irq & A_REQ_TMROUT) { |
| 2833 | + omap_writew(A_REQ_TMROUT, OTG_IRQ_SRC); |
| 2834 | + } else if (otg_irq & A_VBUS_ERR) { |
| 2835 | + omap_writew(A_VBUS_ERR, OTG_IRQ_SRC); |
| 2836 | + } else if (otg_irq & DRIVER_SWITCH) { |
| 2837 | +#ifdef CONFIG_USB_OTG |
| 2838 | + if ((!(omap_readl(OTG_CTRL) & OTG_DRIVER_SEL)) && |
| 2839 | + tu->otg.host && tu->otg.state == OTG_STATE_A_HOST) { |
| 2840 | + /* role is host */ |
| 2841 | + usb_bus_start_enum(tu->otg.host, |
| 2842 | + tu->otg.host->otg_port); |
| 2843 | + } |
| 2844 | +#endif |
| 2845 | + omap_writew(DRIVER_SWITCH, OTG_IRQ_SRC); |
| 2846 | + } else |
| 2847 | + return IRQ_NONE; |
| 2848 | + |
| 2849 | + return IRQ_HANDLED; |
| 2850 | + |
| 2851 | +} |
| 2852 | + |
| 2853 | +static int tahvo_otg_init(void) |
| 2854 | +{ |
| 2855 | + u32 l; |
| 2856 | + |
| 2857 | +#ifdef CONFIG_USB_OTG |
| 2858 | + if (!tahvo_otg_dev) { |
| 2859 | + printk("tahvo-usb: no tahvo_otg_dev\n"); |
| 2860 | + return -ENODEV; |
| 2861 | + } |
| 2862 | +#endif |
| 2863 | + |
| 2864 | + l = omap_readl(OTG_SYSCON_1); |
| 2865 | + l &= ~OTG_IDLE_EN; |
| 2866 | + omap_writel(l, OTG_SYSCON_1); |
| 2867 | + udelay(100); |
| 2868 | + |
| 2869 | + /* some of these values are board-specific... */ |
| 2870 | + l = omap_readl(OTG_SYSCON_2); |
| 2871 | + l |= OTG_EN |
| 2872 | + /* for B-device: */ |
| 2873 | + | SRP_GPDATA /* 9msec Bdev D+ pulse */ |
| 2874 | + | SRP_GPDVBUS /* discharge after VBUS pulse */ |
| 2875 | + // | (3 << 24) /* 2msec VBUS pulse */ |
| 2876 | + /* for A-device: */ |
| 2877 | + | (0 << 20) /* 200ms nominal A_WAIT_VRISE timer */ |
| 2878 | + | SRP_DPW /* detect 167+ns SRP pulses */ |
| 2879 | + | SRP_DATA | SRP_VBUS; /* accept both kinds of SRP pulse */ |
| 2880 | + omap_writel(l, OTG_SYSCON_2); |
| 2881 | + |
| 2882 | + omap_writew(DRIVER_SWITCH | OPRT_CHG |
| 2883 | + | B_SRP_TMROUT | B_HNP_FAIL |
| 2884 | + | A_VBUS_ERR | A_SRP_DETECT | A_REQ_TMROUT, |
| 2885 | + OTG_IRQ_EN); |
| 2886 | + l = omap_readl(OTG_SYSCON_2); |
| 2887 | + l |= OTG_EN; |
| 2888 | + omap_writel(l, OTG_SYSCON_2); |
| 2889 | + |
| 2890 | + return 0; |
| 2891 | +} |
| 2892 | + |
| 2893 | +static int __init omap_otg_probe(struct platform_device *pdev) |
| 2894 | +{ |
| 2895 | + int ret; |
| 2896 | + |
| 2897 | + tahvo_otg_dev = pdev; |
| 2898 | + ret = tahvo_otg_init(); |
| 2899 | + if (ret != 0) { |
| 2900 | + printk(KERN_ERR "tahvo-usb: tahvo_otg_init failed\n"); |
| 2901 | + return ret; |
| 2902 | + } |
| 2903 | + |
| 2904 | + return request_irq(tahvo_otg_dev->resource[1].start, |
| 2905 | + omap_otg_irq, IRQF_DISABLED, DRIVER_NAME, |
| 2906 | + tahvo_usb_device); |
| 2907 | +} |
| 2908 | + |
| 2909 | +static int __exit omap_otg_remove(struct platform_device *pdev) |
| 2910 | +{ |
| 2911 | + free_irq(tahvo_otg_dev->resource[1].start, tahvo_usb_device); |
| 2912 | + tahvo_otg_dev = NULL; |
| 2913 | + |
| 2914 | + return 0; |
| 2915 | +} |
| 2916 | + |
| 2917 | +struct platform_driver omap_otg_driver = { |
| 2918 | + .driver = { |
| 2919 | + .name = "omap_otg", |
| 2920 | + }, |
| 2921 | + .remove = __exit_p(omap_otg_remove), |
| 2922 | +}; |
| 2923 | + |
| 2924 | +/* |
| 2925 | + * --------------------------------------------------------------------------- |
| 2926 | + * Tahvo related functions |
| 2927 | + * These are Nokia proprietary code, except for the OTG register settings, |
| 2928 | + * which are copied from isp1301.c |
| 2929 | + * --------------------------------------------------------------------------- |
| 2930 | + */ |
| 2931 | +static ssize_t vbus_state_show(struct device *device, |
| 2932 | + struct device_attribute *attr, char *buf) |
| 2933 | +{ |
| 2934 | + struct tahvo_usb *tu = dev_get_drvdata(device); |
| 2935 | + return sprintf(buf, "%d\n", tu->vbus_state); |
| 2936 | +} |
| 2937 | +static DEVICE_ATTR(vbus_state, 0444, vbus_state_show, NULL); |
| 2938 | + |
| 2939 | +int vbus_active = 0; |
| 2940 | + |
| 2941 | +static void check_vbus_state(struct tahvo_usb *tu) |
| 2942 | +{ |
| 2943 | + int reg, prev_state; |
| 2944 | + |
| 2945 | + reg = tahvo_read_reg(tu->dev, TAHVO_REG_IDSR); |
| 2946 | + if (reg & 0x01) { |
| 2947 | + u32 l; |
| 2948 | + |
| 2949 | + vbus_active = 1; |
| 2950 | + switch (tu->otg.state) { |
| 2951 | + case OTG_STATE_B_IDLE: |
| 2952 | + /* Enable the gadget driver */ |
| 2953 | + if (tu->otg.gadget) |
| 2954 | + usb_gadget_vbus_connect(tu->otg.gadget); |
| 2955 | + /* Set B-session valid and not B-sessio ended to indicate |
| 2956 | + * Vbus to be ok. */ |
| 2957 | + l = omap_readl(OTG_CTRL); |
| 2958 | + l &= ~OTG_BSESSEND; |
| 2959 | + l |= OTG_BSESSVLD; |
| 2960 | + omap_writel(l, OTG_CTRL); |
| 2961 | + |
| 2962 | + tu->otg.state = OTG_STATE_B_PERIPHERAL; |
| 2963 | + break; |
| 2964 | + case OTG_STATE_A_IDLE: |
| 2965 | + /* Session is now valid assuming the USB hub is driving Vbus */ |
| 2966 | + tu->otg.state = OTG_STATE_A_HOST; |
| 2967 | + break; |
| 2968 | + default: |
| 2969 | + break; |
| 2970 | + } |
| 2971 | + printk("USB cable connected\n"); |
| 2972 | + } else { |
| 2973 | + switch (tu->otg.state) { |
| 2974 | + case OTG_STATE_B_PERIPHERAL: |
| 2975 | + if (tu->otg.gadget) |
| 2976 | + usb_gadget_vbus_disconnect(tu->otg.gadget); |
| 2977 | + tu->otg.state = OTG_STATE_B_IDLE; |
| 2978 | + break; |
| 2979 | + case OTG_STATE_A_HOST: |
| 2980 | + tu->otg.state = OTG_STATE_A_IDLE; |
| 2981 | + break; |
| 2982 | + default: |
| 2983 | + break; |
| 2984 | + } |
| 2985 | + printk("USB cable disconnected\n"); |
| 2986 | + vbus_active = 0; |
| 2987 | + } |
| 2988 | + |
| 2989 | + prev_state = tu->vbus_state; |
| 2990 | + tu->vbus_state = reg & 0x01; |
| 2991 | + if (prev_state != tu->vbus_state) |
| 2992 | + sysfs_notify(&tu->pt_dev->dev.kobj, NULL, "vbus_state"); |
| 2993 | +} |
| 2994 | + |
| 2995 | +static void tahvo_usb_become_host(struct tahvo_usb *tu) |
| 2996 | +{ |
| 2997 | + u32 l; |
| 2998 | + |
| 2999 | + /* Clear system and transceiver controlled bits |
| 3000 | + * also mark the A-session is always valid */ |
| 3001 | + tahvo_otg_init(); |
| 3002 | + |
| 3003 | + l = omap_readl(OTG_CTRL); |
| 3004 | + l &= ~(OTG_CTRL_XCVR_MASK | OTG_CTRL_SYS_MASK); |
| 3005 | + l |= OTG_ASESSVLD; |
| 3006 | + omap_writel(l, OTG_CTRL); |
| 3007 | + |
| 3008 | + /* Power up the transceiver in USB host mode */ |
| 3009 | + tahvo_write_reg(tu->dev, TAHVO_REG_USBR, USBR_REGOUT | USBR_NSUSPEND | |
| 3010 | + USBR_MASTER_SW2 | USBR_MASTER_SW1); |
| 3011 | + tu->otg.state = OTG_STATE_A_IDLE; |
| 3012 | + |
| 3013 | + check_vbus_state(tu); |
| 3014 | +} |
| 3015 | + |
| 3016 | +static void tahvo_usb_stop_host(struct tahvo_usb *tu) |
| 3017 | +{ |
| 3018 | + tu->otg.state = OTG_STATE_A_IDLE; |
| 3019 | +} |
| 3020 | + |
| 3021 | +static void tahvo_usb_become_peripheral(struct tahvo_usb *tu) |
| 3022 | +{ |
| 3023 | + u32 l; |
| 3024 | + |
| 3025 | + /* Clear system and transceiver controlled bits |
| 3026 | + * and enable ID to mark peripheral mode and |
| 3027 | + * BSESSEND to mark no Vbus */ |
| 3028 | + tahvo_otg_init(); |
| 3029 | + l = omap_readl(OTG_CTRL); |
| 3030 | + l &= ~(OTG_CTRL_XCVR_MASK | OTG_CTRL_SYS_MASK | OTG_BSESSVLD); |
| 3031 | + l |= OTG_ID | OTG_BSESSEND; |
| 3032 | + omap_writel(l, OTG_CTRL); |
| 3033 | + |
| 3034 | + /* Power up transceiver and set it in USB perhiperal mode */ |
| 3035 | + tahvo_write_reg(tu->dev, TAHVO_REG_USBR, USBR_SLAVE_CONTROL | USBR_REGOUT | USBR_NSUSPEND | USBR_SLAVE_SW); |
| 3036 | + tu->otg.state = OTG_STATE_B_IDLE; |
| 3037 | + |
| 3038 | + check_vbus_state(tu); |
| 3039 | +} |
| 3040 | + |
| 3041 | +static void tahvo_usb_stop_peripheral(struct tahvo_usb *tu) |
| 3042 | +{ |
| 3043 | + u32 l; |
| 3044 | + |
| 3045 | + l = omap_readl(OTG_CTRL); |
| 3046 | + l &= ~OTG_BSESSVLD; |
| 3047 | + l |= OTG_BSESSEND; |
| 3048 | + omap_writel(l, OTG_CTRL); |
| 3049 | + |
| 3050 | + if (tu->otg.gadget) |
| 3051 | + usb_gadget_vbus_disconnect(tu->otg.gadget); |
| 3052 | + tu->otg.state = OTG_STATE_B_IDLE; |
| 3053 | + |
| 3054 | +} |
| 3055 | + |
| 3056 | +static void tahvo_usb_power_off(struct tahvo_usb *tu) |
| 3057 | +{ |
| 3058 | + u32 l; |
| 3059 | + int id; |
| 3060 | + |
| 3061 | + /* Disable gadget controller if any */ |
| 3062 | + if (tu->otg.gadget) |
| 3063 | + usb_gadget_vbus_disconnect(tu->otg.gadget); |
| 3064 | + |
| 3065 | + /* Disable OTG and interrupts */ |
| 3066 | + if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL) |
| 3067 | + id = OTG_ID; |
| 3068 | + else |
| 3069 | + id = 0; |
| 3070 | + l = omap_readl(OTG_CTRL); |
| 3071 | + l &= ~(OTG_CTRL_XCVR_MASK | OTG_CTRL_SYS_MASK | OTG_BSESSVLD); |
| 3072 | + l |= id | OTG_BSESSEND; |
| 3073 | + omap_writel(l, OTG_CTRL); |
| 3074 | + omap_writew(0, OTG_IRQ_EN); |
| 3075 | + |
| 3076 | + l = omap_readl(OTG_SYSCON_2); |
| 3077 | + l &= ~OTG_EN; |
| 3078 | + omap_writel(l, OTG_SYSCON_2); |
| 3079 | + |
| 3080 | + l = omap_readl(OTG_SYSCON_1); |
| 3081 | + l |= OTG_IDLE_EN; |
| 3082 | + omap_writel(l, OTG_SYSCON_1); |
| 3083 | + |
| 3084 | + /* Power off transceiver */ |
| 3085 | + tahvo_write_reg(tu->dev, TAHVO_REG_USBR, 0); |
| 3086 | + tu->otg.state = OTG_STATE_UNDEFINED; |
| 3087 | +} |
| 3088 | + |
| 3089 | + |
| 3090 | +static int tahvo_usb_set_power(struct otg_transceiver *dev, unsigned mA) |
| 3091 | +{ |
| 3092 | + struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg); |
| 3093 | + |
| 3094 | + dev_dbg(&tu->pt_dev->dev, "set_power %d mA\n", mA); |
| 3095 | + |
| 3096 | + if (dev->state == OTG_STATE_B_PERIPHERAL) { |
| 3097 | + /* REVISIT: Can Tahvo charge battery from VBUS? */ |
| 3098 | + } |
| 3099 | + return 0; |
| 3100 | +} |
| 3101 | + |
| 3102 | +static int tahvo_usb_set_suspend(struct otg_transceiver *dev, int suspend) |
| 3103 | +{ |
| 3104 | + struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg); |
| 3105 | + u16 w; |
| 3106 | + |
| 3107 | + dev_dbg(&tu->pt_dev->dev, "set_suspend\n"); |
| 3108 | + |
| 3109 | + w = tahvo_read_reg(tu->dev, TAHVO_REG_USBR); |
| 3110 | + if (suspend) |
| 3111 | + w &= ~USBR_NSUSPEND; |
| 3112 | + else |
| 3113 | + w |= USBR_NSUSPEND; |
| 3114 | + tahvo_write_reg(tu->dev, TAHVO_REG_USBR, w); |
| 3115 | + |
| 3116 | + return 0; |
| 3117 | +} |
| 3118 | + |
| 3119 | +static int tahvo_usb_start_srp(struct otg_transceiver *dev) |
| 3120 | +{ |
| 3121 | + struct tahvo_usb *tu = container_of(dev, struct tahvo_usb, otg); |
| 3122 | + u32 otg_ctrl; |
| 3123 | + |
| 3124 | + dev_dbg(&tu->pt_dev->dev, "start_srp\n"); |
| 3125 | + |
| 3126 | + if (!dev || tu->otg.state != OTG_STATE_B_IDLE) |
| 3127 | + return -ENODEV; |
| 3128 | + |
| 3129 | + otg_ctrl = omap_readl(OTG_CTRL); |
| 3130 | + if (!(otg_ctrl & OTG_BSESSEND)) |
| 3131 | + return -EINVAL; |
| 3132 | + |
| 3133 | + otg_ctrl |= OTG_B_BUSREQ; |
| 3134 | + otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_SYS_MASK; |
| 3135 | + omap_writel(otg_ctrl, OTG_CTRL); |
| 3136 | + tu->otg.state = OTG_STATE_B_SRP_INIT; |
| 3137 | + |
| 3138 | + return 0; |
| 3139 | +} |
| 3140 | + |
| 3141 | +static int tahvo_usb_start_hnp(struct otg_transceiver *otg) |
| 3142 | +{ |
| 3143 | + struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg); |
| 3144 | + |
| 3145 | + dev_dbg(&tu->pt_dev->dev, "start_hnp\n"); |
| 3146 | +#ifdef CONFIG_USB_OTG |
| 3147 | + /* REVISIT: Add this for OTG */ |
| 3148 | +#endif |
| 3149 | + return -EINVAL; |
| 3150 | +} |
| 3151 | + |
| 3152 | +static int tahvo_usb_set_host(struct otg_transceiver *otg, struct usb_bus *host) |
| 3153 | +{ |
| 3154 | + struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg); |
| 3155 | + u32 l; |
| 3156 | + |
| 3157 | + dev_dbg(&tu->pt_dev->dev, "set_host %p\n", host); |
| 3158 | + |
| 3159 | + if (otg == NULL) |
| 3160 | + return -ENODEV; |
| 3161 | + |
| 3162 | +#if defined(CONFIG_USB_OTG) || !defined(CONFIG_USB_GADGET_OMAP) |
| 3163 | + |
| 3164 | + mutex_lock(&tu->serialize); |
| 3165 | + |
| 3166 | + if (host == NULL) { |
| 3167 | + if (TAHVO_MODE(tu) == TAHVO_MODE_HOST) |
| 3168 | + tahvo_usb_power_off(tu); |
| 3169 | + tu->otg.host = NULL; |
| 3170 | + mutex_unlock(&tu->serialize); |
| 3171 | + return 0; |
| 3172 | + } |
| 3173 | + |
| 3174 | + l = omap_readl(OTG_SYSCON_1); |
| 3175 | + l &= ~(OTG_IDLE_EN | HST_IDLE_EN | DEV_IDLE_EN); |
| 3176 | + omap_writel(l, OTG_SYSCON_1); |
| 3177 | + |
| 3178 | + if (TAHVO_MODE(tu) == TAHVO_MODE_HOST) { |
| 3179 | + tu->otg.host = NULL; |
| 3180 | + tahvo_usb_become_host(tu); |
| 3181 | + } |
| 3182 | + |
| 3183 | + tu->otg.host = host; |
| 3184 | + |
| 3185 | + mutex_unlock(&tu->serialize); |
| 3186 | +#else |
| 3187 | + /* No host mode configured, so do not allow host controlled to be set */ |
| 3188 | + return -EINVAL; |
| 3189 | +#endif |
| 3190 | + |
| 3191 | + return 0; |
| 3192 | +} |
| 3193 | + |
| 3194 | +static int tahvo_usb_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget) |
| 3195 | +{ |
| 3196 | + struct tahvo_usb *tu = container_of(otg, struct tahvo_usb, otg); |
| 3197 | + |
| 3198 | + dev_dbg(&tu->pt_dev->dev, "set_peripheral %p\n", gadget); |
| 3199 | + |
| 3200 | + if (!otg) |
| 3201 | + return -ENODEV; |
| 3202 | + |
| 3203 | +#if defined(CONFIG_USB_OTG) || defined(CONFIG_USB_GADGET_OMAP) |
| 3204 | + |
| 3205 | + mutex_lock(&tu->serialize); |
| 3206 | + |
| 3207 | + if (!gadget) { |
| 3208 | + if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL) |
| 3209 | + tahvo_usb_power_off(tu); |
| 3210 | + tu->otg.gadget = NULL; |
| 3211 | + mutex_unlock(&tu->serialize); |
| 3212 | + return 0; |
| 3213 | + } |
| 3214 | + |
| 3215 | + tu->otg.gadget = gadget; |
| 3216 | + if (TAHVO_MODE(tu) == TAHVO_MODE_PERIPHERAL) |
| 3217 | + tahvo_usb_become_peripheral(tu); |
| 3218 | + |
| 3219 | + mutex_unlock(&tu->serialize); |
| 3220 | +#else |
| 3221 | + /* No gadget mode configured, so do not allow host controlled to be set */ |
| 3222 | + return -EINVAL; |
| 3223 | +#endif |
| 3224 | + |
| 3225 | + return 0; |
| 3226 | +} |
| 3227 | + |
| 3228 | +static irqreturn_t tahvo_usb_vbus_interrupt(int irq, void *_tu) |
| 3229 | +{ |
| 3230 | + struct tahvo_usb *tu = _tu; |
| 3231 | + |
| 3232 | + check_vbus_state(tu); |
| 3233 | + |
| 3234 | + return IRQ_HANDLED; |
| 3235 | +} |
| 3236 | + |
| 3237 | +#ifdef CONFIG_USB_OTG |
| 3238 | +static ssize_t otg_mode_show(struct device *device, |
| 3239 | + struct device_attribute *attr, char *buf) |
| 3240 | +{ |
| 3241 | + struct tahvo_usb *tu = dev_get_drvdata(device); |
| 3242 | + switch (tu->tahvo_mode) { |
| 3243 | + case TAHVO_MODE_HOST: |
| 3244 | + return sprintf(buf, "host\n"); |
| 3245 | + case TAHVO_MODE_PERIPHERAL: |
| 3246 | + return sprintf(buf, "peripheral\n"); |
| 3247 | + } |
| 3248 | + return sprintf(buf, "unknown\n"); |
| 3249 | +} |
| 3250 | + |
| 3251 | +static ssize_t otg_mode_store(struct device *device, |
| 3252 | + struct device_attribute *attr, |
| 3253 | + const char *buf, size_t count) |
| 3254 | +{ |
| 3255 | + struct tahvo_usb *tu = dev_get_drvdata(device); |
| 3256 | + int r; |
| 3257 | + |
| 3258 | + r = strlen(buf); |
| 3259 | + mutex_lock(&tu->serialize); |
| 3260 | + if (strncmp(buf, "host", 4) == 0) { |
| 3261 | + if (tu->tahvo_mode == TAHVO_MODE_PERIPHERAL) |
| 3262 | + tahvo_usb_stop_peripheral(tu); |
| 3263 | + tu->tahvo_mode = TAHVO_MODE_HOST; |
| 3264 | + if (tu->otg.host) { |
| 3265 | + printk(KERN_INFO "Selected HOST mode: host controller present.\n"); |
| 3266 | + tahvo_usb_become_host(tu); |
| 3267 | + } else { |
| 3268 | + printk(KERN_INFO "Selected HOST mode: no host controller, powering off.\n"); |
| 3269 | + tahvo_usb_power_off(tu); |
| 3270 | + } |
| 3271 | + } else if (strncmp(buf, "peripheral", 10) == 0) { |
| 3272 | + if (tu->tahvo_mode == TAHVO_MODE_HOST) |
| 3273 | + tahvo_usb_stop_host(tu); |
| 3274 | + tu->tahvo_mode = TAHVO_MODE_PERIPHERAL; |
| 3275 | + if (tu->otg.gadget) { |
| 3276 | + printk(KERN_INFO "Selected PERIPHERAL mode: gadget driver present.\n"); |
| 3277 | + tahvo_usb_become_peripheral(tu); |
| 3278 | + } else { |
| 3279 | + printk(KERN_INFO "Selected PERIPHERAL mode: no gadget driver, powering off.\n"); |
| 3280 | + tahvo_usb_power_off(tu); |
| 3281 | + } |
| 3282 | + } else |
| 3283 | + r = -EINVAL; |
| 3284 | + |
| 3285 | + mutex_unlock(&tu->serialize); |
| 3286 | + return r; |
| 3287 | +} |
| 3288 | + |
| 3289 | +static DEVICE_ATTR(otg_mode, 0644, otg_mode_show, otg_mode_store); |
| 3290 | +#endif |
| 3291 | + |
| 3292 | +static int __init tahvo_usb_probe(struct platform_device *pdev) |
| 3293 | +{ |
| 3294 | + struct tahvo_usb *tu; |
| 3295 | + struct device *dev = &pdev->dev; |
| 3296 | + int ret; |
| 3297 | + int irq; |
| 3298 | + |
| 3299 | + dev_dbg(dev, "probe\n"); |
| 3300 | + |
| 3301 | + /* Create driver data */ |
| 3302 | + tu = kzalloc(sizeof(*tu), GFP_KERNEL); |
| 3303 | + if (!tu) |
| 3304 | + return -ENOMEM; |
| 3305 | + tahvo_usb_device = tu; |
| 3306 | + |
| 3307 | + tu->dev = dev; |
| 3308 | + tu->pt_dev = pdev; |
| 3309 | +#ifdef CONFIG_USB_OTG |
| 3310 | + /* Default mode */ |
| 3311 | +#ifdef CONFIG_CBUS_TAHVO_USB_HOST_BY_DEFAULT |
| 3312 | + tu->tahvo_mode = TAHVO_MODE_HOST; |
| 3313 | +#else |
| 3314 | + tu->tahvo_mode = TAHVO_MODE_PERIPHERAL; |
| 3315 | +#endif |
| 3316 | +#endif |
| 3317 | + |
| 3318 | + mutex_init(&tu->serialize); |
| 3319 | + |
| 3320 | + tu->ick = clk_get(NULL, "usb_l4_ick"); |
| 3321 | + if (IS_ERR(tu->ick)) { |
| 3322 | + dev_err(dev, "Failed to get usb_l4_ick\n"); |
| 3323 | + ret = PTR_ERR(tu->ick); |
| 3324 | + goto err_free_tu; |
| 3325 | + } |
| 3326 | + clk_enable(tu->ick); |
| 3327 | + |
| 3328 | + /* Set initial state, so that we generate kevents only on |
| 3329 | + * state changes */ |
| 3330 | + tu->vbus_state = tahvo_read_reg(tu->dev, TAHVO_REG_IDSR) & 0x01; |
| 3331 | + |
| 3332 | + irq = platform_get_irq(pdev, 0); |
| 3333 | + tu->irq = irq; |
| 3334 | + |
| 3335 | + /* We cannot enable interrupt until omap_udc is initialized */ |
| 3336 | + ret = request_threaded_irq(irq, NULL, tahvo_usb_vbus_interrupt, |
| 3337 | + IRQF_ONESHOT, "tahvo-vbus", tu); |
| 3338 | + if (ret != 0) { |
| 3339 | + printk(KERN_ERR "Could not register Tahvo interrupt for VBUS\n"); |
| 3340 | + goto err_release_clk; |
| 3341 | + } |
| 3342 | + |
| 3343 | + /* Attributes */ |
| 3344 | + ret = device_create_file(dev, &dev_attr_vbus_state); |
| 3345 | +#ifdef CONFIG_USB_OTG |
| 3346 | + ret |= device_create_file(dev, &dev_attr_otg_mode); |
| 3347 | +#endif |
| 3348 | + if (ret) |
| 3349 | + printk(KERN_ERR "attribute creation failed: %d\n", ret); |
| 3350 | + |
| 3351 | + /* Create OTG interface */ |
| 3352 | + tahvo_usb_power_off(tu); |
| 3353 | + tu->otg.state = OTG_STATE_UNDEFINED; |
| 3354 | + tu->otg.label = DRIVER_NAME; |
| 3355 | + tu->otg.set_host = tahvo_usb_set_host; |
| 3356 | + tu->otg.set_peripheral = tahvo_usb_set_peripheral; |
| 3357 | + tu->otg.set_power = tahvo_usb_set_power; |
| 3358 | + tu->otg.set_suspend = tahvo_usb_set_suspend; |
| 3359 | + tu->otg.start_srp = tahvo_usb_start_srp; |
| 3360 | + tu->otg.start_hnp = tahvo_usb_start_hnp; |
| 3361 | + |
| 3362 | + ret = otg_set_transceiver(&tu->otg); |
| 3363 | + if (ret < 0) { |
| 3364 | + printk(KERN_ERR "Cannot register USB transceiver\n"); |
| 3365 | + goto err_free_irq; |
| 3366 | + } |
| 3367 | + |
| 3368 | + dev_set_drvdata(dev, tu); |
| 3369 | + |
| 3370 | + return 0; |
| 3371 | + |
| 3372 | +err_free_irq: |
| 3373 | + free_irq(tu->irq, tu); |
| 3374 | +err_release_clk: |
| 3375 | + clk_disable(tu->ick); |
| 3376 | + clk_put(tu->ick); |
| 3377 | +err_free_tu: |
| 3378 | + kfree(tu); |
| 3379 | + tahvo_usb_device = NULL; |
| 3380 | + |
| 3381 | + return ret; |
| 3382 | +} |
| 3383 | + |
| 3384 | +static int __exit tahvo_usb_remove(struct platform_device *pdev) |
| 3385 | +{ |
| 3386 | + struct tahvo_usb *tu = platform_get_drvdata(pdev); |
| 3387 | + |
| 3388 | + dev_dbg(&pdev->dev, "remove\n"); |
| 3389 | + |
| 3390 | + free_irq(tu->irq, tu); |
| 3391 | + flush_scheduled_work(); |
| 3392 | + otg_set_transceiver(0); |
| 3393 | + device_remove_file(&pdev->dev, &dev_attr_vbus_state); |
| 3394 | +#ifdef CONFIG_USB_OTG |
| 3395 | + device_remove_file(&pdev->dev, &dev_attr_otg_mode); |
| 3396 | +#endif |
| 3397 | + clk_disable(tu->ick); |
| 3398 | + clk_put(tu->ick); |
| 3399 | + |
| 3400 | + kfree(tu); |
| 3401 | + tahvo_usb_device = NULL; |
| 3402 | + |
| 3403 | + return 0; |
| 3404 | +} |
| 3405 | + |
| 3406 | +static struct platform_driver tahvo_usb_driver = { |
| 3407 | + .driver = { |
| 3408 | + .name = "tahvo-usb", |
| 3409 | + }, |
| 3410 | + .remove = __exit_p(tahvo_usb_remove), |
| 3411 | +}; |
| 3412 | + |
| 3413 | +static int __init tahvo_usb_init(void) |
| 3414 | +{ |
| 3415 | + int ret = 0; |
| 3416 | + |
| 3417 | + ret = platform_driver_probe(&tahvo_usb_driver, tahvo_usb_probe); |
| 3418 | + if (ret) |
| 3419 | + return ret; |
| 3420 | + |
| 3421 | + ret = platform_driver_probe(&omap_otg_driver, omap_otg_probe); |
| 3422 | + if (ret) { |
| 3423 | + platform_driver_unregister(&tahvo_usb_driver); |
| 3424 | + return ret; |
| 3425 | + } |
| 3426 | + |
| 3427 | + return 0; |
| 3428 | +} |
| 3429 | + |
| 3430 | +subsys_initcall(tahvo_usb_init); |
| 3431 | + |
| 3432 | +static void __exit tahvo_usb_exit(void) |
| 3433 | +{ |
| 3434 | + platform_driver_unregister(&omap_otg_driver); |
| 3435 | + platform_driver_unregister(&tahvo_usb_driver); |
| 3436 | +} |
| 3437 | +module_exit(tahvo_usb_exit); |
| 3438 | + |
| 3439 | +MODULE_DESCRIPTION("Tahvo USB OTG Transceiver Driver"); |
| 3440 | +MODULE_LICENSE("GPL"); |
| 3441 | +MODULE_AUTHOR("Juha Yrjölä, Tony Lindgren, and Timo Teräs"); |
| 3442 | --- a/drivers/Makefile |
| 3443 | +++ b/drivers/Makefile |
| 3444 | @@ -78,7 +78,7 @@ obj-$(CONFIG_GAMEPORT) += input/gamepor |
| 3445 | obj-$(CONFIG_INPUT) += input/ |
| 3446 | obj-$(CONFIG_I2O) += message/ |
| 3447 | obj-$(CONFIG_RTC_LIB) += rtc/ |
| 3448 | -obj-y += i2c/ media/ |
| 3449 | +obj-y += i2c/ media/ cbus/ |
| 3450 | obj-$(CONFIG_PPS) += pps/ |
| 3451 | obj-$(CONFIG_PTP_1588_CLOCK) += ptp/ |
| 3452 | obj-$(CONFIG_W1) += w1/ |
| 3453 | --- a/drivers/Kconfig |
| 3454 | +++ b/drivers/Kconfig |
| 3455 | @@ -2,6 +2,8 @@ menu "Device Drivers" |
| 3456 | |
| 3457 | source "drivers/base/Kconfig" |
| 3458 | |
| 3459 | +source "drivers/cbus/Kconfig" |
| 3460 | + |
| 3461 | source "drivers/connector/Kconfig" |
| 3462 | |
| 3463 | source "drivers/mtd/Kconfig" |
| 3464 | |