bus.c 13 KB

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