root/sound/ac97/bus.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. to_ac97_controller
  2. ac97_unbound_ctrl_write
  3. ac97_unbound_ctrl_read
  4. ac97_codec_find
  5. ac97_of_get_child_device
  6. ac97_codec_release
  7. ac97_codec_add
  8. snd_ac97_bus_scan_one
  9. ac97_bus_scan
  10. ac97_bus_reset
  11. snd_ac97_codec_driver_register
  12. snd_ac97_codec_driver_unregister
  13. snd_ac97_codec_get_platdata
  14. ac97_ctrl_codecs_unregister
  15. cold_reset_store
  16. warm_reset_store
  17. ac97_del_adapter
  18. ac97_adapter_release
  19. ac97_add_adapter
  20. snd_ac97_controller_register
  21. snd_ac97_controller_unregister
  22. ac97_pm_runtime_suspend
  23. ac97_pm_runtime_resume
  24. ac97_get_enable_clk
  25. ac97_put_disable_clk
  26. vendor_id_show
  27. ac97_bus_match
  28. ac97_bus_probe
  29. ac97_bus_remove
  30. ac97_bus_init
  31. ac97_bus_exit

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * Copyright (C) 2016 Robert Jarzmik <robert.jarzmik@free.fr>
   4  */
   5 
   6 #include <linux/module.h>
   7 #include <linux/bitops.h>
   8 #include <linux/clk.h>
   9 #include <linux/device.h>
  10 #include <linux/idr.h>
  11 #include <linux/list.h>
  12 #include <linux/mutex.h>
  13 #include <linux/of.h>
  14 #include <linux/pm.h>
  15 #include <linux/pm_runtime.h>
  16 #include <linux/slab.h>
  17 #include <linux/sysfs.h>
  18 #include <sound/ac97/codec.h>
  19 #include <sound/ac97/controller.h>
  20 #include <sound/ac97/regs.h>
  21 
  22 #include "ac97_core.h"
  23 
  24 /*
  25  * Protects ac97_controllers and each ac97_controller structure.
  26  */
  27 static DEFINE_MUTEX(ac97_controllers_mutex);
  28 static DEFINE_IDR(ac97_adapter_idr);
  29 static LIST_HEAD(ac97_controllers);
  30 
  31 static struct bus_type ac97_bus_type;
  32 
  33 static inline struct ac97_controller*
  34 to_ac97_controller(struct device *ac97_adapter)
  35 {
  36         return container_of(ac97_adapter, struct ac97_controller, adap);
  37 }
  38 
  39 static int ac97_unbound_ctrl_write(struct ac97_controller *adrv, int slot,
  40                      unsigned short reg, unsigned short val)
  41 {
  42         return -ENODEV;
  43 }
  44 
  45 static int ac97_unbound_ctrl_read(struct ac97_controller *adrv, int slot,
  46                                   unsigned short reg)
  47 {
  48         return -ENODEV;
  49 }
  50 
  51 static const struct ac97_controller_ops ac97_unbound_ctrl_ops = {
  52         .write = ac97_unbound_ctrl_write,
  53         .read = ac97_unbound_ctrl_read,
  54 };
  55 
  56 static struct ac97_controller ac97_unbound_ctrl = {
  57         .ops = &ac97_unbound_ctrl_ops,
  58 };
  59 
  60 static struct ac97_codec_device *
  61 ac97_codec_find(struct ac97_controller *ac97_ctrl, unsigned int codec_num)
  62 {
  63         if (codec_num >= AC97_BUS_MAX_CODECS)
  64                 return ERR_PTR(-EINVAL);
  65 
  66         return ac97_ctrl->codecs[codec_num];
  67 }
  68 
  69 static struct device_node *
  70 ac97_of_get_child_device(struct ac97_controller *ac97_ctrl, int idx,
  71                          unsigned int vendor_id)
  72 {
  73         struct device_node *node;
  74         u32 reg;
  75         char compat[] = "ac97,0000,0000";
  76 
  77         snprintf(compat, sizeof(compat), "ac97,%04x,%04x",
  78                  vendor_id >> 16, vendor_id & 0xffff);
  79 
  80         for_each_child_of_node(ac97_ctrl->parent->of_node, node) {
  81                 if ((idx != of_property_read_u32(node, "reg", &reg)) ||
  82                     !of_device_is_compatible(node, compat))
  83                         continue;
  84                 return node;
  85         }
  86 
  87         return NULL;
  88 }
  89 
  90 static void ac97_codec_release(struct device *dev)
  91 {
  92         struct ac97_codec_device *adev;
  93         struct ac97_controller *ac97_ctrl;
  94 
  95         adev = to_ac97_device(dev);
  96         ac97_ctrl = adev->ac97_ctrl;
  97         ac97_ctrl->codecs[adev->num] = NULL;
  98         of_node_put(dev->of_node);
  99         kfree(adev);
 100 }
 101 
 102 static int ac97_codec_add(struct ac97_controller *ac97_ctrl, int idx,
 103                    unsigned int vendor_id)
 104 {
 105         struct ac97_codec_device *codec;
 106         int ret;
 107 
 108         codec = kzalloc(sizeof(*codec), GFP_KERNEL);
 109         if (!codec)
 110                 return -ENOMEM;
 111         ac97_ctrl->codecs[idx] = codec;
 112         codec->vendor_id = vendor_id;
 113         codec->dev.release = ac97_codec_release;
 114         codec->dev.bus = &ac97_bus_type;
 115         codec->dev.parent = &ac97_ctrl->adap;
 116         codec->num = idx;
 117         codec->ac97_ctrl = ac97_ctrl;
 118 
 119         device_initialize(&codec->dev);
 120         dev_set_name(&codec->dev, "%s:%u", dev_name(ac97_ctrl->parent), idx);
 121         codec->dev.of_node = ac97_of_get_child_device(ac97_ctrl, idx,
 122                                                       vendor_id);
 123 
 124         ret = device_add(&codec->dev);
 125         if (ret) {
 126                 put_device(&codec->dev);
 127                 return ret;
 128         }
 129 
 130         return 0;
 131 }
 132 
 133 unsigned int snd_ac97_bus_scan_one(struct ac97_controller *adrv,
 134                                    unsigned int codec_num)
 135 {
 136         unsigned short vid1, vid2;
 137         int ret;
 138 
 139         ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID1);
 140         vid1 = (ret & 0xffff);
 141         if (ret < 0)
 142                 return 0;
 143 
 144         ret = adrv->ops->read(adrv, codec_num, AC97_VENDOR_ID2);
 145         vid2 = (ret & 0xffff);
 146         if (ret < 0)
 147                 return 0;
 148 
 149         dev_dbg(&adrv->adap, "%s(codec_num=%u): vendor_id=0x%08x\n",
 150                 __func__, codec_num, AC97_ID(vid1, vid2));
 151         return AC97_ID(vid1, vid2);
 152 }
 153 
 154 static int ac97_bus_scan(struct ac97_controller *ac97_ctrl)
 155 {
 156         int ret, i;
 157         unsigned int vendor_id;
 158 
 159         for (i = 0; i < AC97_BUS_MAX_CODECS; i++) {
 160                 if (ac97_codec_find(ac97_ctrl, i))
 161                         continue;
 162                 if (!(ac97_ctrl->slots_available & BIT(i)))
 163                         continue;
 164                 vendor_id = snd_ac97_bus_scan_one(ac97_ctrl, i);
 165                 if (!vendor_id)
 166                         continue;
 167 
 168                 ret = ac97_codec_add(ac97_ctrl, i, vendor_id);
 169                 if (ret < 0)
 170                         return ret;
 171         }
 172         return 0;
 173 }
 174 
 175 static int ac97_bus_reset(struct ac97_controller *ac97_ctrl)
 176 {
 177         ac97_ctrl->ops->reset(ac97_ctrl);
 178 
 179         return 0;
 180 }
 181 
 182 /**
 183  * snd_ac97_codec_driver_register - register an AC97 codec driver
 184  * @dev: AC97 driver codec to register
 185  *
 186  * Register an AC97 codec driver to the ac97 bus driver, aka. the AC97 digital
 187  * controller.
 188  *
 189  * Returns 0 on success or error code
 190  */
 191 int snd_ac97_codec_driver_register(struct ac97_codec_driver *drv)
 192 {
 193         drv->driver.bus = &ac97_bus_type;
 194         return driver_register(&drv->driver);
 195 }
 196 EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_register);
 197 
 198 /**
 199  * snd_ac97_codec_driver_unregister - unregister an AC97 codec driver
 200  * @dev: AC97 codec driver to unregister
 201  *
 202  * Unregister a previously registered ac97 codec driver.
 203  */
 204 void snd_ac97_codec_driver_unregister(struct ac97_codec_driver *drv)
 205 {
 206         driver_unregister(&drv->driver);
 207 }
 208 EXPORT_SYMBOL_GPL(snd_ac97_codec_driver_unregister);
 209 
 210 /**
 211  * snd_ac97_codec_get_platdata - get platform_data
 212  * @adev: the ac97 codec device
 213  *
 214  * For legacy platforms, in order to have platform_data in codec drivers
 215  * available, while ac97 device are auto-created upon probe, this retrieves the
 216  * platdata which was setup on ac97 controller registration.
 217  *
 218  * Returns the platform data pointer
 219  */
 220 void *snd_ac97_codec_get_platdata(const struct ac97_codec_device *adev)
 221 {
 222         struct ac97_controller *ac97_ctrl = adev->ac97_ctrl;
 223 
 224         return ac97_ctrl->codecs_pdata[adev->num];
 225 }
 226 EXPORT_SYMBOL_GPL(snd_ac97_codec_get_platdata);
 227 
 228 static void ac97_ctrl_codecs_unregister(struct ac97_controller *ac97_ctrl)
 229 {
 230         int i;
 231 
 232         for (i = 0; i < AC97_BUS_MAX_CODECS; i++)
 233                 if (ac97_ctrl->codecs[i]) {
 234                         ac97_ctrl->codecs[i]->ac97_ctrl = &ac97_unbound_ctrl;
 235                         device_unregister(&ac97_ctrl->codecs[i]->dev);
 236                 }
 237 }
 238 
 239 static ssize_t cold_reset_store(struct device *dev,
 240                                 struct device_attribute *attr, const char *buf,
 241                                 size_t len)
 242 {
 243         struct ac97_controller *ac97_ctrl;
 244 
 245         mutex_lock(&ac97_controllers_mutex);
 246         ac97_ctrl = to_ac97_controller(dev);
 247         ac97_ctrl->ops->reset(ac97_ctrl);
 248         mutex_unlock(&ac97_controllers_mutex);
 249         return len;
 250 }
 251 static DEVICE_ATTR_WO(cold_reset);
 252 
 253 static ssize_t warm_reset_store(struct device *dev,
 254                                 struct device_attribute *attr, const char *buf,
 255                                 size_t len)
 256 {
 257         struct ac97_controller *ac97_ctrl;
 258 
 259         if (!dev)
 260                 return -ENODEV;
 261 
 262         mutex_lock(&ac97_controllers_mutex);
 263         ac97_ctrl = to_ac97_controller(dev);
 264         ac97_ctrl->ops->warm_reset(ac97_ctrl);
 265         mutex_unlock(&ac97_controllers_mutex);
 266         return len;
 267 }
 268 static DEVICE_ATTR_WO(warm_reset);
 269 
 270 static struct attribute *ac97_controller_device_attrs[] = {
 271         &dev_attr_cold_reset.attr,
 272         &dev_attr_warm_reset.attr,
 273         NULL
 274 };
 275 
 276 static struct attribute_group ac97_adapter_attr_group = {
 277         .name   = "ac97_operations",
 278         .attrs  = ac97_controller_device_attrs,
 279 };
 280 
 281 static const struct attribute_group *ac97_adapter_groups[] = {
 282         &ac97_adapter_attr_group,
 283         NULL,
 284 };
 285 
 286 static void ac97_del_adapter(struct ac97_controller *ac97_ctrl)
 287 {
 288         mutex_lock(&ac97_controllers_mutex);
 289         ac97_ctrl_codecs_unregister(ac97_ctrl);
 290         list_del(&ac97_ctrl->controllers);
 291         mutex_unlock(&ac97_controllers_mutex);
 292 
 293         device_unregister(&ac97_ctrl->adap);
 294 }
 295 
 296 static void ac97_adapter_release(struct device *dev)
 297 {
 298         struct ac97_controller *ac97_ctrl;
 299 
 300         ac97_ctrl = to_ac97_controller(dev);
 301         idr_remove(&ac97_adapter_idr, ac97_ctrl->nr);
 302         dev_dbg(&ac97_ctrl->adap, "adapter unregistered by %s\n",
 303                 dev_name(ac97_ctrl->parent));
 304 }
 305 
 306 static const struct device_type ac97_adapter_type = {
 307         .groups         = ac97_adapter_groups,
 308         .release        = ac97_adapter_release,
 309 };
 310 
 311 static int ac97_add_adapter(struct ac97_controller *ac97_ctrl)
 312 {
 313         int ret;
 314 
 315         mutex_lock(&ac97_controllers_mutex);
 316         ret = idr_alloc(&ac97_adapter_idr, ac97_ctrl, 0, 0, GFP_KERNEL);
 317         ac97_ctrl->nr = ret;
 318         if (ret >= 0) {
 319                 dev_set_name(&ac97_ctrl->adap, "ac97-%d", ret);
 320                 ac97_ctrl->adap.type = &ac97_adapter_type;
 321                 ac97_ctrl->adap.parent = ac97_ctrl->parent;
 322                 ret = device_register(&ac97_ctrl->adap);
 323                 if (ret)
 324                         put_device(&ac97_ctrl->adap);
 325         }
 326         if (!ret)
 327                 list_add(&ac97_ctrl->controllers, &ac97_controllers);
 328         mutex_unlock(&ac97_controllers_mutex);
 329 
 330         if (!ret)
 331                 dev_dbg(&ac97_ctrl->adap, "adapter registered by %s\n",
 332                         dev_name(ac97_ctrl->parent));
 333         return ret;
 334 }
 335 
 336 /**
 337  * snd_ac97_controller_register - register an ac97 controller
 338  * @ops: the ac97 bus operations
 339  * @dev: the device providing the ac97 DC function
 340  * @slots_available: mask of the ac97 codecs that can be scanned and probed
 341  *                   bit0 => codec 0, bit1 => codec 1 ... bit 3 => codec 3
 342  *
 343  * Register a digital controller which can control up to 4 ac97 codecs. This is
 344  * the controller side of the AC97 AC-link, while the slave side are the codecs.
 345  *
 346  * Returns a valid controller upon success, negative pointer value upon error
 347  */
 348 struct ac97_controller *snd_ac97_controller_register(
 349         const struct ac97_controller_ops *ops, struct device *dev,
 350         unsigned short slots_available, void **codecs_pdata)
 351 {
 352         struct ac97_controller *ac97_ctrl;
 353         int ret, i;
 354 
 355         ac97_ctrl = kzalloc(sizeof(*ac97_ctrl), GFP_KERNEL);
 356         if (!ac97_ctrl)
 357                 return ERR_PTR(-ENOMEM);
 358 
 359         for (i = 0; i < AC97_BUS_MAX_CODECS && codecs_pdata; i++)
 360                 ac97_ctrl->codecs_pdata[i] = codecs_pdata[i];
 361 
 362         ac97_ctrl->ops = ops;
 363         ac97_ctrl->slots_available = slots_available;
 364         ac97_ctrl->parent = dev;
 365         ret = ac97_add_adapter(ac97_ctrl);
 366 
 367         if (ret)
 368                 goto err;
 369         ac97_bus_reset(ac97_ctrl);
 370         ac97_bus_scan(ac97_ctrl);
 371 
 372         return ac97_ctrl;
 373 err:
 374         kfree(ac97_ctrl);
 375         return ERR_PTR(ret);
 376 }
 377 EXPORT_SYMBOL_GPL(snd_ac97_controller_register);
 378 
 379 /**
 380  * snd_ac97_controller_unregister - unregister an ac97 controller
 381  * @ac97_ctrl: the device previously provided to ac97_controller_register()
 382  *
 383  */
 384 void snd_ac97_controller_unregister(struct ac97_controller *ac97_ctrl)
 385 {
 386         ac97_del_adapter(ac97_ctrl);
 387 }
 388 EXPORT_SYMBOL_GPL(snd_ac97_controller_unregister);
 389 
 390 #ifdef CONFIG_PM
 391 static int ac97_pm_runtime_suspend(struct device *dev)
 392 {
 393         struct ac97_codec_device *codec = to_ac97_device(dev);
 394         int ret = pm_generic_runtime_suspend(dev);
 395 
 396         if (ret == 0 && dev->driver) {
 397                 if (pm_runtime_is_irq_safe(dev))
 398                         clk_disable(codec->clk);
 399                 else
 400                         clk_disable_unprepare(codec->clk);
 401         }
 402 
 403         return ret;
 404 }
 405 
 406 static int ac97_pm_runtime_resume(struct device *dev)
 407 {
 408         struct ac97_codec_device *codec = to_ac97_device(dev);
 409         int ret;
 410 
 411         if (dev->driver) {
 412                 if (pm_runtime_is_irq_safe(dev))
 413                         ret = clk_enable(codec->clk);
 414                 else
 415                         ret = clk_prepare_enable(codec->clk);
 416                 if (ret)
 417                         return ret;
 418         }
 419 
 420         return pm_generic_runtime_resume(dev);
 421 }
 422 #endif /* CONFIG_PM */
 423 
 424 static const struct dev_pm_ops ac97_pm = {
 425         .suspend        = pm_generic_suspend,
 426         .resume         = pm_generic_resume,
 427         .freeze         = pm_generic_freeze,
 428         .thaw           = pm_generic_thaw,
 429         .poweroff       = pm_generic_poweroff,
 430         .restore        = pm_generic_restore,
 431         SET_RUNTIME_PM_OPS(
 432                 ac97_pm_runtime_suspend,
 433                 ac97_pm_runtime_resume,
 434                 NULL)
 435 };
 436 
 437 static int ac97_get_enable_clk(struct ac97_codec_device *adev)
 438 {
 439         int ret;
 440 
 441         adev->clk = clk_get(&adev->dev, "ac97_clk");
 442         if (IS_ERR(adev->clk))
 443                 return PTR_ERR(adev->clk);
 444 
 445         ret = clk_prepare_enable(adev->clk);
 446         if (ret)
 447                 clk_put(adev->clk);
 448 
 449         return ret;
 450 }
 451 
 452 static void ac97_put_disable_clk(struct ac97_codec_device *adev)
 453 {
 454         clk_disable_unprepare(adev->clk);
 455         clk_put(adev->clk);
 456 }
 457 
 458 static ssize_t vendor_id_show(struct device *dev,
 459                               struct device_attribute *attr, char *buf)
 460 {
 461         struct ac97_codec_device *codec = to_ac97_device(dev);
 462 
 463         return sprintf(buf, "%08x", codec->vendor_id);
 464 }
 465 DEVICE_ATTR_RO(vendor_id);
 466 
 467 static struct attribute *ac97_dev_attrs[] = {
 468         &dev_attr_vendor_id.attr,
 469         NULL,
 470 };
 471 ATTRIBUTE_GROUPS(ac97_dev);
 472 
 473 static int ac97_bus_match(struct device *dev, struct device_driver *drv)
 474 {
 475         struct ac97_codec_device *adev = to_ac97_device(dev);
 476         struct ac97_codec_driver *adrv = to_ac97_driver(drv);
 477         const struct ac97_id *id = adrv->id_table;
 478         int i = 0;
 479 
 480         if (adev->vendor_id == 0x0 || adev->vendor_id == 0xffffffff)
 481                 return false;
 482 
 483         do {
 484                 if (ac97_ids_match(id[i].id, adev->vendor_id, id[i].mask))
 485                         return true;
 486         } while (id[i++].id);
 487 
 488         return false;
 489 }
 490 
 491 static int ac97_bus_probe(struct device *dev)
 492 {
 493         struct ac97_codec_device *adev = to_ac97_device(dev);
 494         struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
 495         int ret;
 496 
 497         ret = ac97_get_enable_clk(adev);
 498         if (ret)
 499                 return ret;
 500 
 501         pm_runtime_get_noresume(dev);
 502         pm_runtime_set_active(dev);
 503         pm_runtime_enable(dev);
 504 
 505         ret = adrv->probe(adev);
 506         if (ret == 0)
 507                 return 0;
 508 
 509         pm_runtime_disable(dev);
 510         pm_runtime_set_suspended(dev);
 511         pm_runtime_put_noidle(dev);
 512         ac97_put_disable_clk(adev);
 513 
 514         return ret;
 515 }
 516 
 517 static int ac97_bus_remove(struct device *dev)
 518 {
 519         struct ac97_codec_device *adev = to_ac97_device(dev);
 520         struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
 521         int ret;
 522 
 523         ret = pm_runtime_get_sync(dev);
 524         if (ret < 0)
 525                 return ret;
 526 
 527         ret = adrv->remove(adev);
 528         pm_runtime_put_noidle(dev);
 529         if (ret == 0)
 530                 ac97_put_disable_clk(adev);
 531 
 532         pm_runtime_disable(dev);
 533 
 534         return ret;
 535 }
 536 
 537 static struct bus_type ac97_bus_type = {
 538         .name           = "ac97bus",
 539         .dev_groups     = ac97_dev_groups,
 540         .match          = ac97_bus_match,
 541         .pm             = &ac97_pm,
 542         .probe          = ac97_bus_probe,
 543         .remove         = ac97_bus_remove,
 544 };
 545 
 546 static int __init ac97_bus_init(void)
 547 {
 548         return bus_register(&ac97_bus_type);
 549 }
 550 subsys_initcall(ac97_bus_init);
 551 
 552 static void __exit ac97_bus_exit(void)
 553 {
 554         bus_unregister(&ac97_bus_type);
 555 }
 556 module_exit(ac97_bus_exit);
 557 
 558 MODULE_LICENSE("GPL");
 559 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");

/* [<][>][^][v][top][bottom][index][help] */