bus.c 13 KB

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