bus.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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(sizeof(*codec), GFP_KERNEL);
  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. * @dev: 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. * @dev: 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. mutex_lock(&ac97_controllers_mutex);
  207. ac97_ctrl = to_ac97_controller(dev);
  208. ac97_ctrl->ops->reset(ac97_ctrl);
  209. mutex_unlock(&ac97_controllers_mutex);
  210. return len;
  211. }
  212. static DEVICE_ATTR_WO(cold_reset);
  213. static ssize_t warm_reset_store(struct device *dev,
  214. struct device_attribute *attr, const char *buf,
  215. size_t len)
  216. {
  217. struct ac97_controller *ac97_ctrl;
  218. if (!dev)
  219. return -ENODEV;
  220. mutex_lock(&ac97_controllers_mutex);
  221. ac97_ctrl = to_ac97_controller(dev);
  222. ac97_ctrl->ops->warm_reset(ac97_ctrl);
  223. mutex_unlock(&ac97_controllers_mutex);
  224. return len;
  225. }
  226. static DEVICE_ATTR_WO(warm_reset);
  227. static struct attribute *ac97_controller_device_attrs[] = {
  228. &dev_attr_cold_reset.attr,
  229. &dev_attr_warm_reset.attr,
  230. NULL
  231. };
  232. static const struct attribute_group ac97_adapter_attr_group = {
  233. .name = "ac97_operations",
  234. .attrs = ac97_controller_device_attrs,
  235. };
  236. static const struct attribute_group *ac97_adapter_groups[] = {
  237. &ac97_adapter_attr_group,
  238. NULL,
  239. };
  240. static void ac97_del_adapter(struct ac97_controller *ac97_ctrl)
  241. {
  242. mutex_lock(&ac97_controllers_mutex);
  243. ac97_ctrl_codecs_unregister(ac97_ctrl);
  244. list_del(&ac97_ctrl->controllers);
  245. mutex_unlock(&ac97_controllers_mutex);
  246. device_unregister(&ac97_ctrl->adap);
  247. }
  248. static void ac97_adapter_release(struct device *dev)
  249. {
  250. struct ac97_controller *ac97_ctrl;
  251. ac97_ctrl = to_ac97_controller(dev);
  252. idr_remove(&ac97_adapter_idr, ac97_ctrl->nr);
  253. dev_dbg(&ac97_ctrl->adap, "adapter unregistered by %s\n",
  254. dev_name(ac97_ctrl->parent));
  255. }
  256. static const struct device_type ac97_adapter_type = {
  257. .groups = ac97_adapter_groups,
  258. .release = ac97_adapter_release,
  259. };
  260. static int ac97_add_adapter(struct ac97_controller *ac97_ctrl)
  261. {
  262. int ret;
  263. mutex_lock(&ac97_controllers_mutex);
  264. ret = idr_alloc(&ac97_adapter_idr, ac97_ctrl, 0, 0, GFP_KERNEL);
  265. ac97_ctrl->nr = ret;
  266. if (ret >= 0) {
  267. dev_set_name(&ac97_ctrl->adap, "ac97-%d", ret);
  268. ac97_ctrl->adap.type = &ac97_adapter_type;
  269. ac97_ctrl->adap.parent = ac97_ctrl->parent;
  270. ret = device_register(&ac97_ctrl->adap);
  271. if (ret)
  272. put_device(&ac97_ctrl->adap);
  273. }
  274. if (!ret)
  275. list_add(&ac97_ctrl->controllers, &ac97_controllers);
  276. mutex_unlock(&ac97_controllers_mutex);
  277. if (!ret)
  278. dev_dbg(&ac97_ctrl->adap, "adapter registered by %s\n",
  279. dev_name(ac97_ctrl->parent));
  280. return ret;
  281. }
  282. /**
  283. * snd_ac97_controller_register - register an ac97 controller
  284. * @ops: the ac97 bus operations
  285. * @dev: the device providing the ac97 DC function
  286. * @slots_available: mask of the ac97 codecs that can be scanned and probed
  287. * bit0 => codec 0, bit1 => codec 1 ... bit 3 => codec 3
  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(sizeof(*ac97_ctrl), GFP_KERNEL);
  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. goto err;
  311. ac97_bus_reset(ac97_ctrl);
  312. ac97_bus_scan(ac97_ctrl);
  313. return ac97_ctrl;
  314. err:
  315. kfree(ac97_ctrl);
  316. return ERR_PTR(ret);
  317. }
  318. EXPORT_SYMBOL_GPL(snd_ac97_controller_register);
  319. /**
  320. * snd_ac97_controller_unregister - unregister an ac97 controller
  321. * @ac97_ctrl: the device previously provided to ac97_controller_register()
  322. *
  323. */
  324. void snd_ac97_controller_unregister(struct ac97_controller *ac97_ctrl)
  325. {
  326. ac97_del_adapter(ac97_ctrl);
  327. }
  328. EXPORT_SYMBOL_GPL(snd_ac97_controller_unregister);
  329. #ifdef CONFIG_PM
  330. static int ac97_pm_runtime_suspend(struct device *dev)
  331. {
  332. struct ac97_codec_device *codec = to_ac97_device(dev);
  333. int ret = pm_generic_runtime_suspend(dev);
  334. if (ret == 0 && dev->driver) {
  335. if (pm_runtime_is_irq_safe(dev))
  336. clk_disable(codec->clk);
  337. else
  338. clk_disable_unprepare(codec->clk);
  339. }
  340. return ret;
  341. }
  342. static int ac97_pm_runtime_resume(struct device *dev)
  343. {
  344. struct ac97_codec_device *codec = to_ac97_device(dev);
  345. int ret;
  346. if (dev->driver) {
  347. if (pm_runtime_is_irq_safe(dev))
  348. ret = clk_enable(codec->clk);
  349. else
  350. ret = clk_prepare_enable(codec->clk);
  351. if (ret)
  352. return ret;
  353. }
  354. return pm_generic_runtime_resume(dev);
  355. }
  356. #endif /* CONFIG_PM */
  357. static const struct dev_pm_ops ac97_pm = {
  358. .suspend = pm_generic_suspend,
  359. .resume = pm_generic_resume,
  360. .freeze = pm_generic_freeze,
  361. .thaw = pm_generic_thaw,
  362. .poweroff = pm_generic_poweroff,
  363. .restore = pm_generic_restore,
  364. SET_RUNTIME_PM_OPS(
  365. ac97_pm_runtime_suspend,
  366. ac97_pm_runtime_resume,
  367. NULL)
  368. };
  369. static int ac97_get_enable_clk(struct ac97_codec_device *adev)
  370. {
  371. int ret;
  372. adev->clk = clk_get(&adev->dev, "ac97_clk");
  373. if (IS_ERR(adev->clk))
  374. return PTR_ERR(adev->clk);
  375. ret = clk_prepare_enable(adev->clk);
  376. if (ret)
  377. clk_put(adev->clk);
  378. return ret;
  379. }
  380. static void ac97_put_disable_clk(struct ac97_codec_device *adev)
  381. {
  382. clk_disable_unprepare(adev->clk);
  383. clk_put(adev->clk);
  384. }
  385. static ssize_t vendor_id_show(struct device *dev,
  386. struct device_attribute *attr, char *buf)
  387. {
  388. struct ac97_codec_device *codec = to_ac97_device(dev);
  389. return sysfs_emit(buf, "%08x", codec->vendor_id);
  390. }
  391. static DEVICE_ATTR_RO(vendor_id);
  392. static struct attribute *ac97_dev_attrs[] = {
  393. &dev_attr_vendor_id.attr,
  394. NULL,
  395. };
  396. ATTRIBUTE_GROUPS(ac97_dev);
  397. static int ac97_bus_match(struct device *dev, const struct device_driver *drv)
  398. {
  399. struct ac97_codec_device *adev = to_ac97_device(dev);
  400. const struct ac97_codec_driver *adrv = to_ac97_driver(drv);
  401. const struct ac97_id *id = adrv->id_table;
  402. int i = 0;
  403. if (adev->vendor_id == 0x0 || adev->vendor_id == 0xffffffff)
  404. return false;
  405. do {
  406. if (ac97_ids_match(id[i].id, adev->vendor_id, id[i].mask))
  407. return true;
  408. } while (id[i++].id);
  409. return false;
  410. }
  411. static int ac97_bus_probe(struct device *dev)
  412. {
  413. struct ac97_codec_device *adev = to_ac97_device(dev);
  414. struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
  415. int ret;
  416. ret = ac97_get_enable_clk(adev);
  417. if (ret)
  418. return ret;
  419. pm_runtime_get_noresume(dev);
  420. pm_runtime_set_active(dev);
  421. pm_runtime_enable(dev);
  422. ret = adrv->probe(adev);
  423. if (ret == 0)
  424. return 0;
  425. pm_runtime_disable(dev);
  426. pm_runtime_set_suspended(dev);
  427. pm_runtime_put_noidle(dev);
  428. ac97_put_disable_clk(adev);
  429. return ret;
  430. }
  431. static void ac97_bus_remove(struct device *dev)
  432. {
  433. struct ac97_codec_device *adev = to_ac97_device(dev);
  434. struct ac97_codec_driver *adrv = to_ac97_driver(dev->driver);
  435. int ret;
  436. ret = pm_runtime_resume_and_get(dev);
  437. if (ret < 0)
  438. return;
  439. adrv->remove(adev);
  440. pm_runtime_put_noidle(dev);
  441. ac97_put_disable_clk(adev);
  442. pm_runtime_disable(dev);
  443. }
  444. const struct bus_type ac97_bus_type = {
  445. .name = "ac97bus",
  446. .dev_groups = ac97_dev_groups,
  447. .match = ac97_bus_match,
  448. .pm = &ac97_pm,
  449. .probe = ac97_bus_probe,
  450. .remove = ac97_bus_remove,
  451. };
  452. static int __init ac97_bus_init(void)
  453. {
  454. return bus_register(&ac97_bus_type);
  455. }
  456. subsys_initcall(ac97_bus_init);
  457. static void __exit ac97_bus_exit(void)
  458. {
  459. bus_unregister(&ac97_bus_type);
  460. }
  461. module_exit(ac97_bus_exit);
  462. MODULE_DESCRIPTION("AC97 bus interface");
  463. MODULE_LICENSE("GPL");
  464. MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");