bus.c 13 KB

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