Hans-Christian Egtvedt | eb1f293 | 2007-10-16 23:26:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Atmel SSC driver |
| 3 | * |
| 4 | * Copyright (C) 2007 Atmel Corporation |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/platform_device.h> |
| 12 | #include <linux/list.h> |
| 13 | #include <linux/clk.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/io.h> |
Hans-Christian Egtvedt | eb1f293 | 2007-10-16 23:26:11 -0700 | [diff] [blame] | 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/atmel-ssc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Paul Gortmaker | eb12a67 | 2011-07-03 15:14:56 -0400 | [diff] [blame] | 19 | #include <linux/module.h> |
Hans-Christian Egtvedt | eb1f293 | 2007-10-16 23:26:11 -0700 | [diff] [blame] | 20 | |
| 21 | /* Serialize access to ssc_list and user count */ |
| 22 | static DEFINE_SPINLOCK(user_lock); |
| 23 | static LIST_HEAD(ssc_list); |
| 24 | |
| 25 | struct ssc_device *ssc_request(unsigned int ssc_num) |
| 26 | { |
| 27 | int ssc_valid = 0; |
| 28 | struct ssc_device *ssc; |
| 29 | |
| 30 | spin_lock(&user_lock); |
| 31 | list_for_each_entry(ssc, &ssc_list, list) { |
| 32 | if (ssc->pdev->id == ssc_num) { |
| 33 | ssc_valid = 1; |
| 34 | break; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | if (!ssc_valid) { |
| 39 | spin_unlock(&user_lock); |
Hans-Christian Egtvedt | dfecb71 | 2009-02-04 15:12:17 -0800 | [diff] [blame] | 40 | pr_err("ssc: ssc%d platform device is missing\n", ssc_num); |
Hans-Christian Egtvedt | eb1f293 | 2007-10-16 23:26:11 -0700 | [diff] [blame] | 41 | return ERR_PTR(-ENODEV); |
| 42 | } |
| 43 | |
| 44 | if (ssc->user) { |
| 45 | spin_unlock(&user_lock); |
| 46 | dev_dbg(&ssc->pdev->dev, "module busy\n"); |
| 47 | return ERR_PTR(-EBUSY); |
| 48 | } |
| 49 | ssc->user++; |
| 50 | spin_unlock(&user_lock); |
| 51 | |
| 52 | clk_enable(ssc->clk); |
| 53 | |
| 54 | return ssc; |
| 55 | } |
| 56 | EXPORT_SYMBOL(ssc_request); |
| 57 | |
| 58 | void ssc_free(struct ssc_device *ssc) |
| 59 | { |
| 60 | spin_lock(&user_lock); |
| 61 | if (ssc->user) { |
| 62 | ssc->user--; |
| 63 | clk_disable(ssc->clk); |
| 64 | } else { |
| 65 | dev_dbg(&ssc->pdev->dev, "device already free\n"); |
| 66 | } |
| 67 | spin_unlock(&user_lock); |
| 68 | } |
| 69 | EXPORT_SYMBOL(ssc_free); |
| 70 | |
| 71 | static int __init ssc_probe(struct platform_device *pdev) |
| 72 | { |
Hans-Christian Egtvedt | eb1f293 | 2007-10-16 23:26:11 -0700 | [diff] [blame] | 73 | struct resource *regs; |
| 74 | struct ssc_device *ssc; |
| 75 | |
Bo Shen | 2e4de7b | 2012-10-16 11:56:58 +0800 | [diff] [blame^] | 76 | ssc = devm_kzalloc(&pdev->dev, sizeof(struct ssc_device), GFP_KERNEL); |
Hans-Christian Egtvedt | eb1f293 | 2007-10-16 23:26:11 -0700 | [diff] [blame] | 77 | if (!ssc) { |
| 78 | dev_dbg(&pdev->dev, "out of memory\n"); |
Bo Shen | 2e4de7b | 2012-10-16 11:56:58 +0800 | [diff] [blame^] | 79 | return -ENOMEM; |
Hans-Christian Egtvedt | eb1f293 | 2007-10-16 23:26:11 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Bo Shen | 2e4de7b | 2012-10-16 11:56:58 +0800 | [diff] [blame^] | 82 | ssc->pdev = pdev; |
| 83 | |
Hans-Christian Egtvedt | eb1f293 | 2007-10-16 23:26:11 -0700 | [diff] [blame] | 84 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 85 | if (!regs) { |
| 86 | dev_dbg(&pdev->dev, "no mmio resource defined\n"); |
Bo Shen | 2e4de7b | 2012-10-16 11:56:58 +0800 | [diff] [blame^] | 87 | return -ENXIO; |
Hans-Christian Egtvedt | eb1f293 | 2007-10-16 23:26:11 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Bo Shen | 2e4de7b | 2012-10-16 11:56:58 +0800 | [diff] [blame^] | 90 | ssc->regs = devm_request_and_ioremap(&pdev->dev, regs); |
Hans-Christian Egtvedt | eb1f293 | 2007-10-16 23:26:11 -0700 | [diff] [blame] | 91 | if (!ssc->regs) { |
| 92 | dev_dbg(&pdev->dev, "ioremap failed\n"); |
Bo Shen | 2e4de7b | 2012-10-16 11:56:58 +0800 | [diff] [blame^] | 93 | return -EINVAL; |
| 94 | } |
| 95 | |
| 96 | ssc->clk = devm_clk_get(&pdev->dev, "pclk"); |
| 97 | if (IS_ERR(ssc->clk)) { |
| 98 | dev_dbg(&pdev->dev, "no pclk clock defined\n"); |
| 99 | return -ENXIO; |
Hans-Christian Egtvedt | eb1f293 | 2007-10-16 23:26:11 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* disable all interrupts */ |
| 103 | clk_enable(ssc->clk); |
| 104 | ssc_writel(ssc->regs, IDR, ~0UL); |
| 105 | ssc_readl(ssc->regs, SR); |
| 106 | clk_disable(ssc->clk); |
| 107 | |
| 108 | ssc->irq = platform_get_irq(pdev, 0); |
| 109 | if (!ssc->irq) { |
| 110 | dev_dbg(&pdev->dev, "could not get irq\n"); |
Bo Shen | 2e4de7b | 2012-10-16 11:56:58 +0800 | [diff] [blame^] | 111 | return -ENXIO; |
Hans-Christian Egtvedt | eb1f293 | 2007-10-16 23:26:11 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | spin_lock(&user_lock); |
| 115 | list_add_tail(&ssc->list, &ssc_list); |
| 116 | spin_unlock(&user_lock); |
| 117 | |
| 118 | platform_set_drvdata(pdev, ssc); |
| 119 | |
| 120 | dev_info(&pdev->dev, "Atmel SSC device at 0x%p (irq %d)\n", |
| 121 | ssc->regs, ssc->irq); |
| 122 | |
Bo Shen | 2e4de7b | 2012-10-16 11:56:58 +0800 | [diff] [blame^] | 123 | return 0; |
Hans-Christian Egtvedt | eb1f293 | 2007-10-16 23:26:11 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static int __devexit ssc_remove(struct platform_device *pdev) |
| 127 | { |
| 128 | struct ssc_device *ssc = platform_get_drvdata(pdev); |
| 129 | |
| 130 | spin_lock(&user_lock); |
Hans-Christian Egtvedt | eb1f293 | 2007-10-16 23:26:11 -0700 | [diff] [blame] | 131 | list_del(&ssc->list); |
Hans-Christian Egtvedt | eb1f293 | 2007-10-16 23:26:11 -0700 | [diff] [blame] | 132 | spin_unlock(&user_lock); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static struct platform_driver ssc_driver = { |
| 138 | .remove = __devexit_p(ssc_remove), |
| 139 | .driver = { |
| 140 | .name = "ssc", |
Kay Sievers | d6c2385 | 2008-04-15 14:34:33 -0700 | [diff] [blame] | 141 | .owner = THIS_MODULE, |
Hans-Christian Egtvedt | eb1f293 | 2007-10-16 23:26:11 -0700 | [diff] [blame] | 142 | }, |
| 143 | }; |
| 144 | |
| 145 | static int __init ssc_init(void) |
| 146 | { |
| 147 | return platform_driver_probe(&ssc_driver, ssc_probe); |
| 148 | } |
| 149 | module_init(ssc_init); |
| 150 | |
| 151 | static void __exit ssc_exit(void) |
| 152 | { |
| 153 | platform_driver_unregister(&ssc_driver); |
| 154 | } |
| 155 | module_exit(ssc_exit); |
| 156 | |
| 157 | MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>"); |
| 158 | MODULE_DESCRIPTION("SSC driver for Atmel AVR32 and AT91"); |
| 159 | MODULE_LICENSE("GPL"); |
Kay Sievers | d6c2385 | 2008-04-15 14:34:33 -0700 | [diff] [blame] | 160 | MODULE_ALIAS("platform:ssc"); |