0001
0002
0003
0004
0005
0006 #include <linux/module.h>
0007 #include <linux/bitops.h>
0008 #include <linux/clk.h>
0009 #include <linux/device.h>
0010 #include <linux/idr.h>
0011 #include <linux/list.h>
0012 #include <linux/mutex.h>
0013 #include <linux/of.h>
0014 #include <linux/pm.h>
0015 #include <linux/pm_runtime.h>
0016 #include <linux/slab.h>
0017 #include <linux/sysfs.h>
0018 #include <sound/ac97/codec.h>
0019 #include <sound/ac97/controller.h>
0020 #include <sound/ac97/regs.h>
0021
0022 #include "ac97_core.h"
0023
0024
0025
0026
0027 static DEFINE_MUTEX(ac97_controllers_mutex);
0028 static DEFINE_IDR(ac97_adapter_idr);
0029 static LIST_HEAD(ac97_controllers);
0030
0031 static struct bus_type ac97_bus_type;
0032
0033 static inline struct ac97_controller*
0034 to_ac97_controller(struct device *ac97_adapter)
0035 {
0036 return container_of(ac97_adapter, struct ac97_controller, adap);
0037 }
0038
0039 static int ac97_unbound_ctrl_write(struct ac97_controller *adrv, int slot,
0040 unsigned short reg, unsigned short val)
0041 {
0042 return -ENODEV;
0043 }
0044
0045 static int ac97_unbound_ctrl_read(struct ac97_controller *adrv, int slot,
0046 unsigned short reg)
0047 {
0048 return -ENODEV;
0049 }
0050
0051 static const struct ac97_controller_ops ac97_unbound_ctrl_ops = {
0052 .write = ac97_unbound_ctrl_write,
0053 .read = ac97_unbound_ctrl_read,
0054 };
0055
0056 static struct ac97_controller ac97_unbound_ctrl = {
0057 .ops = &ac97_unbound_ctrl_ops,
0058 };
0059
0060 static struct ac97_codec_device *
0061 ac97_codec_find(struct ac97_controller *ac97_ctrl, unsigned int codec_num)
0062 {
0063 if (codec_num >= AC97_BUS_MAX_CODECS)
0064 return ERR_PTR(-EINVAL);
0065
0066 return ac97_ctrl->codecs[codec_num];
0067 }
0068
0069 static struct device_node *
0070 ac97_of_get_child_device(struct ac97_controller *ac97_ctrl, int idx,
0071 unsigned int vendor_id)
0072 {
0073 struct device_node *node;
0074 u32 reg;
0075 char compat[] = "ac97,0000,0000";
0076
0077 snprintf(compat, sizeof(compat), "ac97,%04x,%04x",
0078 vendor_id >> 16, vendor_id & 0xffff);
0079
0080 for_each_child_of_node(ac97_ctrl->parent->of_node, node) {
0081 if ((idx != of_property_read_u32(node, "reg", ®)) ||
0082 !of_device_is_compatible(node, compat))
0083 continue;
0084 return node;
0085 }
0086
0087 return NULL;
0088 }
0089
0090 static void ac97_codec_release(struct device *dev)
0091 {
0092 struct ac97_codec_device *adev;
0093 struct ac97_controller *ac97_ctrl;
0094
0095 adev = to_ac97_device(dev);
0096 ac97_ctrl = adev->ac97_ctrl;
0097 ac97_ctrl->codecs[adev->num] = NULL;
0098 of_node_put(dev->of_node);
0099 kfree(adev);
0100 }
0101
0102 static int ac97_codec_add(struct ac97_controller *ac97_ctrl, int idx,
0103 unsigned int vendor_id)
0104 {
0105 struct ac97_codec_device *codec;
0106 int ret;
0107
0108 codec = kzalloc(sizeof(*codec), GFP_KERNEL);
0109 if (!codec)
0110 return -ENOMEM;
0111 ac97_ctrl->codecs[idx] = codec;
0112 codec->vendor_id = vendor_id;
0113 codec->dev.release = ac97_codec_release;
0114 codec->dev.bus = &ac97_bus_type;
0115 codec->dev.parent = &ac97_ctrl->adap;
0116 codec->num = idx;
0117 codec->ac97_ctrl = ac97_ctrl;
0118
0119 device_initialize(&codec->dev);
0120 dev_set_name(&codec->dev, "%s:%u", dev_name(ac97_ctrl->parent), idx);
0121 codec->dev.of_node = ac97_of_get_child_device(ac97_ctrl, idx,
0122 vendor_id);
0123
0124 ret = device_add(&codec->dev);
0125 if (ret) {
0126 put_device(&codec->dev);
0127 return ret;
0128 }
0129
0130 return 0;
0131 }
0132
0133 unsigned int snd_ac97_bus_scan_one(struct ac97_controller *adrv,
0134 unsigned int codec_num)
0135 {
0136 unsigned short vid1, vid2;
0137 int ret;
0138
0139 ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID1);
0140 vid1 = (ret & 0xffff);
0141 if (ret < 0)
0142 return 0;
0143
0144 ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID2);
0145 vid2 = (ret & 0xffff);
0146 if (ret < 0)
0147 return 0;
0148
0149 dev_dbg(&adrv->adap, "%s(codec_num=%u): vendor_id=0x%08x\n",
0150 __func__, codec_num, AC97_ID(vid1, vid2));
0151 return AC97_ID(vid1, vid2);
0152 }
0153
0154 static int ac97_bus_scan(struct ac97_controller *ac97_ctrl)
0155 {
0156 int ret, i;
0157 unsigned int vendor_id;
0158
0159 for (i = 0; i < AC97_BUS_MAX_CODECS; i++) {
0160 if (ac97_codec_find(ac97_ctrl, i))
0161 continue;
0162 if (!(ac97_ctrl->slots_available & BIT(i)))
0163 continue;
0164 vendor_id = snd_ac97_bus_scan_one(ac97_ctrl, i);
0165 if (!vendor_id)
0166 continue;
0167
0168 ret = ac97_codec_add(ac97_ctrl, i, vendor_id);
0169 if (ret < 0)
0170 return ret;
0171 }
0172 return 0;
0173 }
0174
0175 static int ac97_bus_reset(struct ac97_controller *ac97_ctrl)
0176 {
0177 ac97_ctrl->ops->reset(ac97_ctrl);
0178
0179 return 0;
0180 }
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191 int snd_ac97_codec_driver_register(struct ac97_codec_driver *drv)
0192 {
0193 drv->driver.bus = &ac97_bus_type;
0194 return driver_register(&drv->driver);
0195 }
0196 EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_register);
0197
0198
0199
0200
0201
0202
0203
0204 void snd_ac97_codec_driver_unregister(struct ac97_codec_driver *drv)
0205 {
0206 driver_unregister(&drv->driver);
0207 }
0208 EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_unregister);
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220 void *snd_ac97_codec_get_platdata(const struct ac97_codec_device *adev)
0221 {
0222 struct ac97_controller *ac97_ctrl = adev->ac97_ctrl;
0223
0224 return ac97_ctrl->codecs_pdata[adev->num];
0225 }
0226 EXPORT_SYMBOL_GPL(snd_ac97_codec_get_platdata);
0227
0228 static void ac97_ctrl_codecs_unregister(struct ac97_controller *ac97_ctrl)
0229 {
0230 int i;
0231
0232 for (i = 0; i < AC97_BUS_MAX_CODECS; i++)
0233 if (ac97_ctrl->codecs[i]) {
0234 ac97_ctrl->codecs[i]->ac97_ctrl = &ac97_unbound_ctrl;
0235 device_unregister(&ac97_ctrl->codecs[i]->dev);
0236 }
0237 }
0238
0239 static ssize_t cold_reset_store(struct device *dev,
0240 struct device_attribute *attr, const char *buf,
0241 size_t len)
0242 {
0243 struct ac97_controller *ac97_ctrl;
0244
0245 mutex_lock(&ac97_controllers_mutex);
0246 ac97_ctrl = to_ac97_controller(dev);
0247 ac97_ctrl->ops->reset(ac97_ctrl);
0248 mutex_unlock(&ac97_controllers_mutex);
0249 return len;
0250 }
0251 static DEVICE_ATTR_WO(cold_reset);
0252
0253 static ssize_t warm_reset_store(struct device *dev,
0254 struct device_attribute *attr, const char *buf,
0255 size_t len)
0256 {
0257 struct ac97_controller *ac97_ctrl;
0258
0259 if (!dev)
0260 return -ENODEV;
0261
0262 mutex_lock(&ac97_controllers_mutex);
0263 ac97_ctrl = to_ac97_controller(dev);
0264 ac97_ctrl->ops->warm_reset(ac97_ctrl);
0265 mutex_unlock(&ac97_controllers_mutex);
0266 return len;
0267 }
0268 static DEVICE_ATTR_WO(warm_reset);
0269
0270 static struct attribute *ac97_controller_device_attrs[] = {
0271 &dev_attr_cold_reset.attr,
0272 &dev_attr_warm_reset.attr,
0273 NULL
0274 };
0275
0276 static const struct attribute_group ac97_adapter_attr_group = {
0277 .name = "ac97_operations",
0278 .attrs = ac97_controller_device_attrs,
0279 };
0280
0281 static const struct attribute_group *ac97_adapter_groups[] = {
0282 &ac97_adapter_attr_group,
0283 NULL,
0284 };
0285
0286 static void ac97_del_adapter(struct ac97_controller *ac97_ctrl)
0287 {
0288 mutex_lock(&ac97_controllers_mutex);
0289 ac97_ctrl_codecs_unregister(ac97_ctrl);
0290 list_del(&ac97_ctrl->controllers);
0291 mutex_unlock(&ac97_controllers_mutex);
0292
0293 device_unregister(&ac97_ctrl->adap);
0294 }
0295
0296 static void ac97_adapter_release(struct device *dev)
0297 {
0298 struct ac97_controller *ac97_ctrl;
0299
0300 ac97_ctrl = to_ac97_controller(dev);
0301 idr_remove(&ac97_adapter_idr, ac97_ctrl->nr);
0302 dev_dbg(&ac97_ctrl->adap, "adapter unregistered by %s\n",
0303 dev_name(ac97_ctrl->parent));
0304 }
0305
0306 static const struct device_type ac97_adapter_type = {
0307 .groups = ac97_adapter_groups,
0308 .release = ac97_adapter_release,
0309 };
0310
0311 static int ac97_add_adapter(struct ac97_controller *ac97_ctrl)
0312 {
0313 int ret;
0314
0315 mutex_lock(&ac97_controllers_mutex);
0316 ret = idr_alloc(&ac97_adapter_idr, ac97_ctrl, 0, 0, GFP_KERNEL);
0317 ac97_ctrl->nr = ret;
0318 if (ret >= 0) {
0319 dev_set_name(&ac97_ctrl->adap, "ac97-%d", ret);
0320 ac97_ctrl->adap.type = &ac97_adapter_type;
0321 ac97_ctrl->adap.parent = ac97_ctrl->parent;
0322 ret = device_register(&ac97_ctrl->adap);
0323 if (ret)
0324 put_device(&ac97_ctrl->adap);
0325 }
0326 if (!ret)
0327 list_add(&ac97_ctrl->controllers, &ac97_controllers);
0328 mutex_unlock(&ac97_controllers_mutex);
0329
0330 if (!ret)
0331 dev_dbg(&ac97_ctrl->adap, "adapter registered by %s\n",
0332 dev_name(ac97_ctrl->parent));
0333 return ret;
0334 }
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348 struct ac97_controller *snd_ac97_controller_register(
0349 const struct ac97_controller_ops *ops, struct device *dev,
0350 unsigned short slots_available, void **codecs_pdata)
0351 {
0352 struct ac97_controller *ac97_ctrl;
0353 int ret, i;
0354
0355 ac97_ctrl = kzalloc(sizeof(*ac97_ctrl), GFP_KERNEL);
0356 if (!ac97_ctrl)
0357 return ERR_PTR(-ENOMEM);
0358
0359 for (i = 0; i < AC97_BUS_MAX_CODECS && codecs_pdata; i++)
0360 ac97_ctrl->codecs_pdata[i] = codecs_pdata[i];
0361
0362 ac97_ctrl->ops = ops;
0363 ac97_ctrl->slots_available = slots_available;
0364 ac97_ctrl->parent = dev;
0365 ret = ac97_add_adapter(ac97_ctrl);
0366
0367 if (ret)
0368 goto err;
0369 ac97_bus_reset(ac97_ctrl);
0370 ac97_bus_scan(ac97_ctrl);
0371
0372 return ac97_ctrl;
0373 err:
0374 kfree(ac97_ctrl);
0375 return ERR_PTR(ret);
0376 }
0377 EXPORT_SYMBOL_GPL(snd_ac97_controller_register);
0378
0379
0380
0381
0382
0383
0384 void snd_ac97_controller_unregister(struct ac97_controller *ac97_ctrl)
0385 {
0386 ac97_del_adapter(ac97_ctrl);
0387 }
0388 EXPORT_SYMBOL_GPL(snd_ac97_controller_unregister);
0389
0390 #ifdef CONFIG_PM
0391 static int ac97_pm_runtime_suspend(struct device *dev)
0392 {
0393 struct ac97_codec_device *codec = to_ac97_device(dev);
0394 int ret = pm_generic_runtime_suspend(dev);
0395
0396 if (ret == 0 && dev->driver) {
0397 if (pm_runtime_is_irq_safe(dev))
0398 clk_disable(codec->clk);
0399 else
0400 clk_disable_unprepare(codec->clk);
0401 }
0402
0403 return ret;
0404 }
0405
0406 static int ac97_pm_runtime_resume(struct device *dev)
0407 {
0408 struct ac97_codec_device *codec = to_ac97_device(dev);
0409 int ret;
0410
0411 if (dev->driver) {
0412 if (pm_runtime_is_irq_safe(dev))
0413 ret = clk_enable(codec->clk);
0414 else
0415 ret = clk_prepare_enable(codec->clk);
0416 if (ret)
0417 return ret;
0418 }
0419
0420 return pm_generic_runtime_resume(dev);
0421 }
0422 #endif
0423
0424 static const struct dev_pm_ops ac97_pm = {
0425 .suspend = pm_generic_suspend,
0426 .resume = pm_generic_resume,
0427 .freeze = pm_generic_freeze,
0428 .thaw = pm_generic_thaw,
0429 .poweroff = pm_generic_poweroff,
0430 .restore = pm_generic_restore,
0431 SET_RUNTIME_PM_OPS(
0432 ac97_pm_runtime_suspend,
0433 ac97_pm_runtime_resume,
0434 NULL)
0435 };
0436
0437 static int ac97_get_enable_clk(struct ac97_codec_device *adev)
0438 {
0439 int ret;
0440
0441 adev->clk = clk_get(&adev->dev, "ac97_clk");
0442 if (IS_ERR(adev->clk))
0443 return PTR_ERR(adev->clk);
0444
0445 ret = clk_prepare_enable(adev->clk);
0446 if (ret)
0447 clk_put(adev->clk);
0448
0449 return ret;
0450 }
0451
0452 static void ac97_put_disable_clk(struct ac97_codec_device *adev)
0453 {
0454 clk_disable_unprepare(adev->clk);
0455 clk_put(adev->clk);
0456 }
0457
0458 static ssize_t vendor_id_show(struct device *dev,
0459 struct device_attribute *attr, char *buf)
0460 {
0461 struct ac97_codec_device *codec = to_ac97_device(dev);
0462
0463 return sysfs_emit(buf, "%08x", codec->vendor_id);
0464 }
0465 DEVICE_ATTR_RO(vendor_id);
0466
0467 static struct attribute *ac97_dev_attrs[] = {
0468 &dev_attr_vendor_id.attr,
0469 NULL,
0470 };
0471 ATTRIBUTE_GROUPS(ac97_dev);
0472
0473 static int ac97_bus_match(struct device *dev, struct device_driver *drv)
0474 {
0475 struct ac97_codec_device *adev = to_ac97_device(dev);
0476 struct ac97_codec_driver *adrv = to_ac97_driver(drv);
0477 const struct ac97_id *id = adrv->id_table;
0478 int i = 0;
0479
0480 if (adev->vendor_id == 0x0 || adev->vendor_id == 0xffffffff)
0481 return false;
0482
0483 do {
0484 if (ac97_ids_match(id[i].id, adev->vendor_id, id[i].mask))
0485 return true;
0486 } while (id[i++].id);
0487
0488 return false;
0489 }
0490
0491 static int ac97_bus_probe(struct device *dev)
0492 {
0493 struct ac97_codec_device *adev = to_ac97_device(dev);
0494 struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
0495 int ret;
0496
0497 ret = ac97_get_enable_clk(adev);
0498 if (ret)
0499 return ret;
0500
0501 pm_runtime_get_noresume(dev);
0502 pm_runtime_set_active(dev);
0503 pm_runtime_enable(dev);
0504
0505 ret = adrv->probe(adev);
0506 if (ret == 0)
0507 return 0;
0508
0509 pm_runtime_disable(dev);
0510 pm_runtime_set_suspended(dev);
0511 pm_runtime_put_noidle(dev);
0512 ac97_put_disable_clk(adev);
0513
0514 return ret;
0515 }
0516
0517 static void ac97_bus_remove(struct device *dev)
0518 {
0519 struct ac97_codec_device *adev = to_ac97_device(dev);
0520 struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
0521 int ret;
0522
0523 ret = pm_runtime_resume_and_get(dev);
0524 if (ret < 0)
0525 return;
0526
0527 ret = adrv->remove(adev);
0528 pm_runtime_put_noidle(dev);
0529 if (ret == 0)
0530 ac97_put_disable_clk(adev);
0531
0532 pm_runtime_disable(dev);
0533 }
0534
0535 static struct bus_type ac97_bus_type = {
0536 .name = "ac97bus",
0537 .dev_groups = ac97_dev_groups,
0538 .match = ac97_bus_match,
0539 .pm = &ac97_pm,
0540 .probe = ac97_bus_probe,
0541 .remove = ac97_bus_remove,
0542 };
0543
0544 static int __init ac97_bus_init(void)
0545 {
0546 return bus_register(&ac97_bus_type);
0547 }
0548 subsys_initcall(ac97_bus_init);
0549
0550 static void __exit ac97_bus_exit(void)
0551 {
0552 bus_unregister(&ac97_bus_type);
0553 }
0554 module_exit(ac97_bus_exit);
0555
0556 MODULE_LICENSE("GPL");
0557 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");