1 /* 2 * linux/drivers/mfd/mcp-core.c 3 * 4 * Copyright (C) 2001 Russell King 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 as published by 8 * the Free Software Foundation; either version 2 of the License. 9 * 10 * Generic MCP (Multimedia Communications Port) layer. All MCP locking 11 * is solely held within this file. 12 */ 13 #include <linux/module.h> 14 #include <linux/init.h> 15 #include <linux/errno.h> 16 #include <linux/smp.h> 17 #include <linux/device.h> 18 #include <linux/slab.h> 19 #include <linux/string.h> 20 #include <linux/mfd/mcp.h> 21 22 #include <mach/dma.h> 23 #include <asm/system.h> 24 25 26 #define to_mcp(d) container_of(d, struct mcp, attached_device) 27 #define to_mcp_driver(d) container_of(d, struct mcp_driver, drv) 28 29 static const struct mcp_device_id *mcp_match_id(const struct mcp_device_id *id, 30 const char *codec) 31 { 32 while (id->name[0]) { 33 if (strcmp(codec, id->name) == 0) 34 return id; 35 id++; 36 } 37 return NULL; 38 } 39 40 const struct mcp_device_id *mcp_get_device_id(const struct mcp *mcp) 41 { 42 const struct mcp_driver *driver = 43 to_mcp_driver(mcp->attached_device.driver); 44 45 return mcp_match_id(driver->id_table, mcp->codec); 46 } 47 EXPORT_SYMBOL(mcp_get_device_id); 48 49 static int mcp_bus_match(struct device *dev, struct device_driver *drv) 50 { 51 const struct mcp *mcp = to_mcp(dev); 52 const struct mcp_driver *driver = to_mcp_driver(drv); 53 54 if (driver->id_table) 55 return !!mcp_match_id(driver->id_table, mcp->codec); 56 57 return 0; 58 } 59 60 static int mcp_bus_probe(struct device *dev) 61 { 62 struct mcp *mcp = to_mcp(dev); 63 struct mcp_driver *drv = to_mcp_driver(dev->driver); 64 65 return drv->probe(mcp); 66 } 67 68 static int mcp_bus_remove(struct device *dev) 69 { 70 struct mcp *mcp = to_mcp(dev); 71 struct mcp_driver *drv = to_mcp_driver(dev->driver); 72 73 drv->remove(mcp); 74 return 0; 75 } 76 77 static int mcp_bus_suspend(struct device *dev, pm_message_t state) 78 { 79 struct mcp *mcp = to_mcp(dev); 80 int ret = 0; 81 82 if (dev->driver) { 83 struct mcp_driver *drv = to_mcp_driver(dev->driver); 84 85 ret = drv->suspend(mcp, state); 86 } 87 return ret; 88 } 89 90 static int mcp_bus_resume(struct device *dev) 91 { 92 struct mcp *mcp = to_mcp(dev); 93 int ret = 0; 94 95 if (dev->driver) { 96 struct mcp_driver *drv = to_mcp_driver(dev->driver); 97 98 ret = drv->resume(mcp); 99 } 100 return ret; 101 } 102 103 static int mcp_bus_uevent(struct device *dev, struct kobj_uevent_env *env) 104 { 105 struct mcp *mcp = to_mcp(dev); 106 107 add_uevent_var(env, "MODALIAS=%s%s", MCP_MODULE_PREFIX, mcp->codec); 108 return 0; 109 } 110 111 static struct bus_type mcp_bus_type = { 112 .name = "mcp", 113 .match = mcp_bus_match, 114 .uevent = mcp_bus_uevent, 115 .probe = mcp_bus_probe, 116 .remove = mcp_bus_remove, 117 .suspend = mcp_bus_suspend, 118 .resume = mcp_bus_resume, 119 }; 120 121 /** 122 * mcp_set_telecom_divisor - set the telecom divisor 123 * @mcp: MCP interface structure 124 * @div: SIB clock divisor 125 * 126 * Set the telecom divisor on the MCP interface. The resulting 127 * sample rate is SIBCLOCK/div. 128 */ 129 void mcp_set_telecom_divisor(struct mcp *mcp, unsigned int div) 130 { 131 spin_lock_irq(&mcp->lock); 132 mcp->ops->set_telecom_divisor(mcp, div); 133 spin_unlock_irq(&mcp->lock); 134 } 135 EXPORT_SYMBOL(mcp_set_telecom_divisor); 136 137 /** 138 * mcp_set_audio_divisor - set the audio divisor 139 * @mcp: MCP interface structure 140 * @div: SIB clock divisor 141 * 142 * Set the audio divisor on the MCP interface. 143 */ 144 void mcp_set_audio_divisor(struct mcp *mcp, unsigned int div) 145 { 146 spin_lock_irq(&mcp->lock); 147 mcp->ops->set_audio_divisor(mcp, div); 148 spin_unlock_irq(&mcp->lock); 149 } 150 EXPORT_SYMBOL(mcp_set_audio_divisor); 151 152 /** 153 * mcp_reg_write - write a device register 154 * @mcp: MCP interface structure 155 * @reg: 4-bit register index 156 * @val: 16-bit data value 157 * 158 * Write a device register. The MCP interface must be enabled 159 * to prevent this function hanging. 160 */ 161 void mcp_reg_write(struct mcp *mcp, unsigned int reg, unsigned int val) 162 { 163 unsigned long flags; 164 165 spin_lock_irqsave(&mcp->lock, flags); 166 mcp->ops->reg_write(mcp, reg, val); 167 spin_unlock_irqrestore(&mcp->lock, flags); 168 } 169 EXPORT_SYMBOL(mcp_reg_write); 170 171 /** 172 * mcp_reg_read - read a device register 173 * @mcp: MCP interface structure 174 * @reg: 4-bit register index 175 * 176 * Read a device register and return its value. The MCP interface 177 * must be enabled to prevent this function hanging. 178 */ 179 unsigned int mcp_reg_read(struct mcp *mcp, unsigned int reg) 180 { 181 unsigned long flags; 182 unsigned int val; 183 184 spin_lock_irqsave(&mcp->lock, flags); 185 val = mcp->ops->reg_read(mcp, reg); 186 spin_unlock_irqrestore(&mcp->lock, flags); 187 188 return val; 189 } 190 EXPORT_SYMBOL(mcp_reg_read); 191 192 /** 193 * mcp_enable - enable the MCP interface 194 * @mcp: MCP interface to enable 195 * 196 * Enable the MCP interface. Each call to mcp_enable will need 197 * a corresponding call to mcp_disable to disable the interface. 198 */ 199 void mcp_enable(struct mcp *mcp) 200 { 201 spin_lock_irq(&mcp->lock); 202 if (mcp->use_count++ == 0) 203 mcp->ops->enable(mcp); 204 spin_unlock_irq(&mcp->lock); 205 } 206 EXPORT_SYMBOL(mcp_enable); 207 208 /** 209 * mcp_disable - disable the MCP interface 210 * @mcp: MCP interface to disable 211 * 212 * Disable the MCP interface. The MCP interface will only be 213 * disabled once the number of calls to mcp_enable matches the 214 * number of calls to mcp_disable. 215 */ 216 void mcp_disable(struct mcp *mcp) 217 { 218 unsigned long flags; 219 220 spin_lock_irqsave(&mcp->lock, flags); 221 if (--mcp->use_count == 0) 222 mcp->ops->disable(mcp); 223 spin_unlock_irqrestore(&mcp->lock, flags); 224 } 225 EXPORT_SYMBOL(mcp_disable); 226 227 static void mcp_release(struct device *dev) 228 { 229 struct mcp *mcp = container_of(dev, struct mcp, attached_device); 230 231 kfree(mcp); 232 } 233 234 struct mcp *mcp_host_alloc(struct device *parent, size_t size) 235 { 236 struct mcp *mcp; 237 238 mcp = kzalloc(sizeof(struct mcp) + size, GFP_KERNEL); 239 if (mcp) { 240 spin_lock_init(&mcp->lock); 241 mcp->attached_device.parent = parent; 242 mcp->attached_device.bus = &mcp_bus_type; 243 mcp->attached_device.dma_mask = parent->dma_mask; 244 mcp->attached_device.release = mcp_release; 245 } 246 return mcp; 247 } 248 EXPORT_SYMBOL(mcp_host_alloc); 249 250 int mcp_host_register(struct mcp *mcp, void *pdata) 251 { 252 if (!mcp->codec) 253 return -EINVAL; 254 255 mcp->attached_device.platform_data = pdata; 256 dev_set_name(&mcp->attached_device, "mcp0"); 257 request_module("%s%s", MCP_MODULE_PREFIX, mcp->codec); 258 return device_register(&mcp->attached_device); 259 } 260 EXPORT_SYMBOL(mcp_host_register); 261 262 void mcp_host_unregister(struct mcp *mcp) 263 { 264 device_unregister(&mcp->attached_device); 265 } 266 EXPORT_SYMBOL(mcp_host_unregister); 267 268 int mcp_driver_register(struct mcp_driver *mcpdrv) 269 { 270 mcpdrv->drv.bus = &mcp_bus_type; 271 return driver_register(&mcpdrv->drv); 272 } 273 EXPORT_SYMBOL(mcp_driver_register); 274 275 void mcp_driver_unregister(struct mcp_driver *mcpdrv) 276 { 277 driver_unregister(&mcpdrv->drv); 278 } 279 EXPORT_SYMBOL(mcp_driver_unregister); 280 281 static int __init mcp_init(void) 282 { 283 return bus_register(&mcp_bus_type); 284 } 285 286 static void __exit mcp_exit(void) 287 { 288 bus_unregister(&mcp_bus_type); 289 } 290 291 module_init(mcp_init); 292 module_exit(mcp_exit); 293 294 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>"); 295 MODULE_DESCRIPTION("Core multimedia communications port driver"); 296 MODULE_LICENSE("GPL"); 297