sunxi-rsb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * RSB (Reduced Serial Bus) driver.
  4. *
  5. * Author: Chen-Yu Tsai <wens@csie.org>
  6. *
  7. * The RSB controller looks like an SMBus controller which only supports
  8. * byte and word data transfers. But, it differs from standard SMBus
  9. * protocol on several aspects:
  10. * - it uses addresses set at runtime to address slaves. Runtime addresses
  11. * are sent to slaves using their 12bit hardware addresses. Up to 15
  12. * runtime addresses are available.
  13. * - it adds a parity bit every 8bits of data and address for read and
  14. * write accesses; this replaces the ack bit
  15. * - only one read access is required to read a byte (instead of a write
  16. * followed by a read access in standard SMBus protocol)
  17. * - there's no Ack bit after each read access
  18. *
  19. * This means this bus cannot be used to interface with standard SMBus
  20. * devices. Devices known to support this interface include the AXP223,
  21. * AXP809, and AXP806 PMICs, and the AC100 audio codec, all from X-Powers.
  22. *
  23. * A description of the operation and wire protocol can be found in the
  24. * RSB section of Allwinner's A80 user manual, which can be found at
  25. *
  26. * https://github.com/allwinner-zh/documents/tree/master/A80
  27. *
  28. * This document is officially released by Allwinner.
  29. *
  30. * This driver is based on i2c-sun6i-p2wi.c, the P2WI bus driver.
  31. */
  32. #include <linux/clk.h>
  33. #include <linux/clk/clk-conf.h>
  34. #include <linux/device.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/io.h>
  37. #include <linux/iopoll.h>
  38. #include <linux/module.h>
  39. #include <linux/of.h>
  40. #include <linux/of_irq.h>
  41. #include <linux/of_device.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/pm.h>
  44. #include <linux/pm_runtime.h>
  45. #include <linux/regmap.h>
  46. #include <linux/reset.h>
  47. #include <linux/slab.h>
  48. #include <linux/sunxi-rsb.h>
  49. #include <linux/types.h>
  50. /* RSB registers */
  51. #define RSB_CTRL 0x0 /* Global control */
  52. #define RSB_CCR 0x4 /* Clock control */
  53. #define RSB_INTE 0x8 /* Interrupt controls */
  54. #define RSB_INTS 0xc /* Interrupt status */
  55. #define RSB_ADDR 0x10 /* Address to send with read/write command */
  56. #define RSB_DATA 0x1c /* Data to read/write */
  57. #define RSB_LCR 0x24 /* Line control */
  58. #define RSB_DMCR 0x28 /* Device mode (init) control */
  59. #define RSB_CMD 0x2c /* RSB Command */
  60. #define RSB_DAR 0x30 /* Device address / runtime address */
  61. /* CTRL fields */
  62. #define RSB_CTRL_START_TRANS BIT(7)
  63. #define RSB_CTRL_ABORT_TRANS BIT(6)
  64. #define RSB_CTRL_GLOBAL_INT_ENB BIT(1)
  65. #define RSB_CTRL_SOFT_RST BIT(0)
  66. /* CLK CTRL fields */
  67. #define RSB_CCR_SDA_OUT_DELAY(v) (((v) & 0x7) << 8)
  68. #define RSB_CCR_MAX_CLK_DIV 0xff
  69. #define RSB_CCR_CLK_DIV(v) ((v) & RSB_CCR_MAX_CLK_DIV)
  70. /* STATUS fields */
  71. #define RSB_INTS_TRANS_ERR_ACK BIT(16)
  72. #define RSB_INTS_TRANS_ERR_DATA_BIT(v) (((v) >> 8) & 0xf)
  73. #define RSB_INTS_TRANS_ERR_DATA GENMASK(11, 8)
  74. #define RSB_INTS_LOAD_BSY BIT(2)
  75. #define RSB_INTS_TRANS_ERR BIT(1)
  76. #define RSB_INTS_TRANS_OVER BIT(0)
  77. /* LINE CTRL fields*/
  78. #define RSB_LCR_SCL_STATE BIT(5)
  79. #define RSB_LCR_SDA_STATE BIT(4)
  80. #define RSB_LCR_SCL_CTL BIT(3)
  81. #define RSB_LCR_SCL_CTL_EN BIT(2)
  82. #define RSB_LCR_SDA_CTL BIT(1)
  83. #define RSB_LCR_SDA_CTL_EN BIT(0)
  84. /* DEVICE MODE CTRL field values */
  85. #define RSB_DMCR_DEVICE_START BIT(31)
  86. #define RSB_DMCR_MODE_DATA (0x7c << 16)
  87. #define RSB_DMCR_MODE_REG (0x3e << 8)
  88. #define RSB_DMCR_DEV_ADDR 0x00
  89. /* CMD values */
  90. #define RSB_CMD_RD8 0x8b
  91. #define RSB_CMD_RD16 0x9c
  92. #define RSB_CMD_RD32 0xa6
  93. #define RSB_CMD_WR8 0x4e
  94. #define RSB_CMD_WR16 0x59
  95. #define RSB_CMD_WR32 0x63
  96. #define RSB_CMD_STRA 0xe8
  97. /* DAR fields */
  98. #define RSB_DAR_RTA(v) (((v) & 0xff) << 16)
  99. #define RSB_DAR_DA(v) ((v) & 0xffff)
  100. #define RSB_MAX_FREQ 20000000
  101. #define RSB_CTRL_NAME "sunxi-rsb"
  102. struct sunxi_rsb_addr_map {
  103. u16 hwaddr;
  104. u8 rtaddr;
  105. };
  106. struct sunxi_rsb {
  107. struct device *dev;
  108. void __iomem *regs;
  109. struct clk *clk;
  110. struct reset_control *rstc;
  111. struct completion complete;
  112. struct mutex lock;
  113. unsigned int status;
  114. u32 clk_freq;
  115. };
  116. /* bus / slave device related functions */
  117. static const struct bus_type sunxi_rsb_bus;
  118. static int sunxi_rsb_device_match(struct device *dev, const struct device_driver *drv)
  119. {
  120. return of_driver_match_device(dev, drv);
  121. }
  122. static int sunxi_rsb_device_probe(struct device *dev)
  123. {
  124. const struct sunxi_rsb_driver *drv = to_sunxi_rsb_driver(dev->driver);
  125. struct sunxi_rsb_device *rdev = to_sunxi_rsb_device(dev);
  126. int ret;
  127. if (!drv->probe)
  128. return -ENODEV;
  129. if (!rdev->irq) {
  130. int irq = -ENOENT;
  131. if (dev->of_node)
  132. irq = of_irq_get(dev->of_node, 0);
  133. if (irq == -EPROBE_DEFER)
  134. return irq;
  135. if (irq < 0)
  136. irq = 0;
  137. rdev->irq = irq;
  138. }
  139. ret = of_clk_set_defaults(dev->of_node, false);
  140. if (ret < 0)
  141. return ret;
  142. return drv->probe(rdev);
  143. }
  144. static void sunxi_rsb_device_remove(struct device *dev)
  145. {
  146. const struct sunxi_rsb_driver *drv = to_sunxi_rsb_driver(dev->driver);
  147. drv->remove(to_sunxi_rsb_device(dev));
  148. }
  149. static int sunxi_rsb_device_modalias(const struct device *dev, struct kobj_uevent_env *env)
  150. {
  151. return of_device_uevent_modalias(dev, env);
  152. }
  153. static const struct bus_type sunxi_rsb_bus = {
  154. .name = RSB_CTRL_NAME,
  155. .match = sunxi_rsb_device_match,
  156. .probe = sunxi_rsb_device_probe,
  157. .remove = sunxi_rsb_device_remove,
  158. .uevent = sunxi_rsb_device_modalias,
  159. };
  160. static void sunxi_rsb_dev_release(struct device *dev)
  161. {
  162. struct sunxi_rsb_device *rdev = to_sunxi_rsb_device(dev);
  163. kfree(rdev);
  164. }
  165. /**
  166. * sunxi_rsb_device_create() - allocate and add an RSB device
  167. * @rsb: RSB controller
  168. * @node: RSB slave device node
  169. * @hwaddr: RSB slave hardware address
  170. * @rtaddr: RSB slave runtime address
  171. */
  172. static struct sunxi_rsb_device *sunxi_rsb_device_create(struct sunxi_rsb *rsb,
  173. struct device_node *node, u16 hwaddr, u8 rtaddr)
  174. {
  175. int err;
  176. struct sunxi_rsb_device *rdev;
  177. rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
  178. if (!rdev)
  179. return ERR_PTR(-ENOMEM);
  180. rdev->rsb = rsb;
  181. rdev->hwaddr = hwaddr;
  182. rdev->rtaddr = rtaddr;
  183. rdev->dev.bus = &sunxi_rsb_bus;
  184. rdev->dev.parent = rsb->dev;
  185. rdev->dev.of_node = node;
  186. rdev->dev.release = sunxi_rsb_dev_release;
  187. dev_set_name(&rdev->dev, "%s-%x", RSB_CTRL_NAME, hwaddr);
  188. err = device_register(&rdev->dev);
  189. if (err < 0) {
  190. dev_err(&rdev->dev, "Can't add %s, status %d\n",
  191. dev_name(&rdev->dev), err);
  192. goto err_device_add;
  193. }
  194. dev_dbg(&rdev->dev, "device %s registered\n", dev_name(&rdev->dev));
  195. return rdev;
  196. err_device_add:
  197. put_device(&rdev->dev);
  198. return ERR_PTR(err);
  199. }
  200. /**
  201. * sunxi_rsb_device_unregister(): unregister an RSB device
  202. * @rdev: rsb_device to be removed
  203. */
  204. static void sunxi_rsb_device_unregister(struct sunxi_rsb_device *rdev)
  205. {
  206. device_unregister(&rdev->dev);
  207. }
  208. static int sunxi_rsb_remove_devices(struct device *dev, void *data)
  209. {
  210. struct sunxi_rsb_device *rdev = to_sunxi_rsb_device(dev);
  211. if (dev->bus == &sunxi_rsb_bus)
  212. sunxi_rsb_device_unregister(rdev);
  213. return 0;
  214. }
  215. /**
  216. * sunxi_rsb_driver_register() - Register device driver with RSB core
  217. * @rdrv: device driver to be associated with slave-device.
  218. *
  219. * This API will register the client driver with the RSB framework.
  220. * It is typically called from the driver's module-init function.
  221. */
  222. int sunxi_rsb_driver_register(struct sunxi_rsb_driver *rdrv)
  223. {
  224. rdrv->driver.bus = &sunxi_rsb_bus;
  225. return driver_register(&rdrv->driver);
  226. }
  227. EXPORT_SYMBOL_GPL(sunxi_rsb_driver_register);
  228. /* common code that starts a transfer */
  229. static int _sunxi_rsb_run_xfer(struct sunxi_rsb *rsb)
  230. {
  231. u32 int_mask, status;
  232. bool timeout;
  233. if (readl(rsb->regs + RSB_CTRL) & RSB_CTRL_START_TRANS) {
  234. dev_dbg(rsb->dev, "RSB transfer still in progress\n");
  235. return -EBUSY;
  236. }
  237. reinit_completion(&rsb->complete);
  238. int_mask = RSB_INTS_LOAD_BSY | RSB_INTS_TRANS_ERR | RSB_INTS_TRANS_OVER;
  239. writel(int_mask, rsb->regs + RSB_INTE);
  240. writel(RSB_CTRL_START_TRANS | RSB_CTRL_GLOBAL_INT_ENB,
  241. rsb->regs + RSB_CTRL);
  242. if (irqs_disabled()) {
  243. timeout = readl_poll_timeout_atomic(rsb->regs + RSB_INTS,
  244. status, (status & int_mask),
  245. 10, 100000);
  246. writel(status, rsb->regs + RSB_INTS);
  247. } else {
  248. timeout = !wait_for_completion_io_timeout(&rsb->complete,
  249. msecs_to_jiffies(100));
  250. status = rsb->status;
  251. }
  252. if (timeout) {
  253. dev_dbg(rsb->dev, "RSB timeout\n");
  254. /* abort the transfer */
  255. writel(RSB_CTRL_ABORT_TRANS, rsb->regs + RSB_CTRL);
  256. /* clear any interrupt flags */
  257. writel(readl(rsb->regs + RSB_INTS), rsb->regs + RSB_INTS);
  258. return -ETIMEDOUT;
  259. }
  260. if (status & RSB_INTS_LOAD_BSY) {
  261. dev_dbg(rsb->dev, "RSB busy\n");
  262. return -EBUSY;
  263. }
  264. if (status & RSB_INTS_TRANS_ERR) {
  265. if (status & RSB_INTS_TRANS_ERR_ACK) {
  266. dev_dbg(rsb->dev, "RSB slave nack\n");
  267. return -EINVAL;
  268. }
  269. if (status & RSB_INTS_TRANS_ERR_DATA) {
  270. dev_dbg(rsb->dev, "RSB transfer data error\n");
  271. return -EIO;
  272. }
  273. }
  274. return 0;
  275. }
  276. static int sunxi_rsb_read(struct sunxi_rsb *rsb, u8 rtaddr, u8 addr,
  277. u32 *buf, size_t len)
  278. {
  279. u32 cmd;
  280. int ret;
  281. if (!buf)
  282. return -EINVAL;
  283. switch (len) {
  284. case 1:
  285. cmd = RSB_CMD_RD8;
  286. break;
  287. case 2:
  288. cmd = RSB_CMD_RD16;
  289. break;
  290. case 4:
  291. cmd = RSB_CMD_RD32;
  292. break;
  293. default:
  294. dev_err(rsb->dev, "Invalid access width: %zd\n", len);
  295. return -EINVAL;
  296. }
  297. ret = pm_runtime_resume_and_get(rsb->dev);
  298. if (ret)
  299. return ret;
  300. mutex_lock(&rsb->lock);
  301. writel(addr, rsb->regs + RSB_ADDR);
  302. writel(RSB_DAR_RTA(rtaddr), rsb->regs + RSB_DAR);
  303. writel(cmd, rsb->regs + RSB_CMD);
  304. ret = _sunxi_rsb_run_xfer(rsb);
  305. if (ret)
  306. goto unlock;
  307. *buf = readl(rsb->regs + RSB_DATA) & GENMASK(len * 8 - 1, 0);
  308. unlock:
  309. mutex_unlock(&rsb->lock);
  310. pm_runtime_mark_last_busy(rsb->dev);
  311. pm_runtime_put_autosuspend(rsb->dev);
  312. return ret;
  313. }
  314. static int sunxi_rsb_write(struct sunxi_rsb *rsb, u8 rtaddr, u8 addr,
  315. const u32 *buf, size_t len)
  316. {
  317. u32 cmd;
  318. int ret;
  319. if (!buf)
  320. return -EINVAL;
  321. switch (len) {
  322. case 1:
  323. cmd = RSB_CMD_WR8;
  324. break;
  325. case 2:
  326. cmd = RSB_CMD_WR16;
  327. break;
  328. case 4:
  329. cmd = RSB_CMD_WR32;
  330. break;
  331. default:
  332. dev_err(rsb->dev, "Invalid access width: %zd\n", len);
  333. return -EINVAL;
  334. }
  335. ret = pm_runtime_resume_and_get(rsb->dev);
  336. if (ret)
  337. return ret;
  338. mutex_lock(&rsb->lock);
  339. writel(addr, rsb->regs + RSB_ADDR);
  340. writel(RSB_DAR_RTA(rtaddr), rsb->regs + RSB_DAR);
  341. writel(*buf, rsb->regs + RSB_DATA);
  342. writel(cmd, rsb->regs + RSB_CMD);
  343. ret = _sunxi_rsb_run_xfer(rsb);
  344. mutex_unlock(&rsb->lock);
  345. pm_runtime_mark_last_busy(rsb->dev);
  346. pm_runtime_put_autosuspend(rsb->dev);
  347. return ret;
  348. }
  349. /* RSB regmap functions */
  350. struct sunxi_rsb_ctx {
  351. struct sunxi_rsb_device *rdev;
  352. int size;
  353. };
  354. static int regmap_sunxi_rsb_reg_read(void *context, unsigned int reg,
  355. unsigned int *val)
  356. {
  357. struct sunxi_rsb_ctx *ctx = context;
  358. struct sunxi_rsb_device *rdev = ctx->rdev;
  359. if (reg > 0xff)
  360. return -EINVAL;
  361. return sunxi_rsb_read(rdev->rsb, rdev->rtaddr, reg, val, ctx->size);
  362. }
  363. static int regmap_sunxi_rsb_reg_write(void *context, unsigned int reg,
  364. unsigned int val)
  365. {
  366. struct sunxi_rsb_ctx *ctx = context;
  367. struct sunxi_rsb_device *rdev = ctx->rdev;
  368. return sunxi_rsb_write(rdev->rsb, rdev->rtaddr, reg, &val, ctx->size);
  369. }
  370. static void regmap_sunxi_rsb_free_ctx(void *context)
  371. {
  372. struct sunxi_rsb_ctx *ctx = context;
  373. kfree(ctx);
  374. }
  375. static const struct regmap_bus regmap_sunxi_rsb = {
  376. .reg_write = regmap_sunxi_rsb_reg_write,
  377. .reg_read = regmap_sunxi_rsb_reg_read,
  378. .free_context = regmap_sunxi_rsb_free_ctx,
  379. .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
  380. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  381. };
  382. static struct sunxi_rsb_ctx *regmap_sunxi_rsb_init_ctx(struct sunxi_rsb_device *rdev,
  383. const struct regmap_config *config)
  384. {
  385. struct sunxi_rsb_ctx *ctx;
  386. switch (config->val_bits) {
  387. case 8:
  388. case 16:
  389. case 32:
  390. break;
  391. default:
  392. return ERR_PTR(-EINVAL);
  393. }
  394. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  395. if (!ctx)
  396. return ERR_PTR(-ENOMEM);
  397. ctx->rdev = rdev;
  398. ctx->size = config->val_bits / 8;
  399. return ctx;
  400. }
  401. struct regmap *__devm_regmap_init_sunxi_rsb(struct sunxi_rsb_device *rdev,
  402. const struct regmap_config *config,
  403. struct lock_class_key *lock_key,
  404. const char *lock_name)
  405. {
  406. struct sunxi_rsb_ctx *ctx = regmap_sunxi_rsb_init_ctx(rdev, config);
  407. if (IS_ERR(ctx))
  408. return ERR_CAST(ctx);
  409. return __devm_regmap_init(&rdev->dev, &regmap_sunxi_rsb, ctx, config,
  410. lock_key, lock_name);
  411. }
  412. EXPORT_SYMBOL_GPL(__devm_regmap_init_sunxi_rsb);
  413. /* RSB controller driver functions */
  414. static irqreturn_t sunxi_rsb_irq(int irq, void *dev_id)
  415. {
  416. struct sunxi_rsb *rsb = dev_id;
  417. u32 status;
  418. status = readl(rsb->regs + RSB_INTS);
  419. rsb->status = status;
  420. /* Clear interrupts */
  421. status &= (RSB_INTS_LOAD_BSY | RSB_INTS_TRANS_ERR |
  422. RSB_INTS_TRANS_OVER);
  423. writel(status, rsb->regs + RSB_INTS);
  424. complete(&rsb->complete);
  425. return IRQ_HANDLED;
  426. }
  427. static int sunxi_rsb_init_device_mode(struct sunxi_rsb *rsb)
  428. {
  429. int ret = 0;
  430. u32 reg;
  431. /* send init sequence */
  432. writel(RSB_DMCR_DEVICE_START | RSB_DMCR_MODE_DATA |
  433. RSB_DMCR_MODE_REG | RSB_DMCR_DEV_ADDR, rsb->regs + RSB_DMCR);
  434. readl_poll_timeout(rsb->regs + RSB_DMCR, reg,
  435. !(reg & RSB_DMCR_DEVICE_START), 100, 250000);
  436. if (reg & RSB_DMCR_DEVICE_START)
  437. ret = -ETIMEDOUT;
  438. /* clear interrupt status bits */
  439. writel(readl(rsb->regs + RSB_INTS), rsb->regs + RSB_INTS);
  440. return ret;
  441. }
  442. /*
  443. * There are 15 valid runtime addresses, though Allwinner typically
  444. * skips the first, for unknown reasons, and uses the following three.
  445. *
  446. * 0x17, 0x2d, 0x3a, 0x4e, 0x59, 0x63, 0x74, 0x8b,
  447. * 0x9c, 0xa6, 0xb1, 0xc5, 0xd2, 0xe8, 0xff
  448. *
  449. * No designs with 2 RSB slave devices sharing identical hardware
  450. * addresses on the same bus have been seen in the wild. All designs
  451. * use 0x2d for the primary PMIC, 0x3a for the secondary PMIC if
  452. * there is one, and 0x45 for peripheral ICs.
  453. *
  454. * The hardware does not seem to support re-setting runtime addresses.
  455. * Attempts to do so result in the slave devices returning a NACK.
  456. * Hence we just hardcode the mapping here, like Allwinner does.
  457. */
  458. static const struct sunxi_rsb_addr_map sunxi_rsb_addr_maps[] = {
  459. { 0x3a3, 0x2d }, /* Primary PMIC: AXP223, AXP809, AXP81X, ... */
  460. { 0x745, 0x3a }, /* Secondary PMIC: AXP806, ... */
  461. { 0xe89, 0x4e }, /* Peripheral IC: AC100, ... */
  462. };
  463. static u8 sunxi_rsb_get_rtaddr(u16 hwaddr)
  464. {
  465. int i;
  466. for (i = 0; i < ARRAY_SIZE(sunxi_rsb_addr_maps); i++)
  467. if (hwaddr == sunxi_rsb_addr_maps[i].hwaddr)
  468. return sunxi_rsb_addr_maps[i].rtaddr;
  469. return 0; /* 0 is an invalid runtime address */
  470. }
  471. static int of_rsb_register_devices(struct sunxi_rsb *rsb)
  472. {
  473. struct device *dev = rsb->dev;
  474. struct device_node *child, *np = dev->of_node;
  475. u32 hwaddr;
  476. u8 rtaddr;
  477. int ret;
  478. if (!np)
  479. return -EINVAL;
  480. /* Runtime addresses for all slaves should be set first */
  481. for_each_available_child_of_node(np, child) {
  482. dev_dbg(dev, "setting child %pOF runtime address\n",
  483. child);
  484. ret = of_property_read_u32(child, "reg", &hwaddr);
  485. if (ret) {
  486. dev_err(dev, "%pOF: invalid 'reg' property: %d\n",
  487. child, ret);
  488. continue;
  489. }
  490. rtaddr = sunxi_rsb_get_rtaddr(hwaddr);
  491. if (!rtaddr) {
  492. dev_err(dev, "%pOF: unknown hardware device address\n",
  493. child);
  494. continue;
  495. }
  496. /*
  497. * Since no devices have been registered yet, we are the
  498. * only ones using the bus, we can skip locking the bus.
  499. */
  500. /* setup command parameters */
  501. writel(RSB_CMD_STRA, rsb->regs + RSB_CMD);
  502. writel(RSB_DAR_RTA(rtaddr) | RSB_DAR_DA(hwaddr),
  503. rsb->regs + RSB_DAR);
  504. /* send command */
  505. ret = _sunxi_rsb_run_xfer(rsb);
  506. if (ret)
  507. dev_warn(dev, "%pOF: set runtime address failed: %d\n",
  508. child, ret);
  509. }
  510. /* Then we start adding devices and probing them */
  511. for_each_available_child_of_node(np, child) {
  512. struct sunxi_rsb_device *rdev;
  513. dev_dbg(dev, "adding child %pOF\n", child);
  514. ret = of_property_read_u32(child, "reg", &hwaddr);
  515. if (ret)
  516. continue;
  517. rtaddr = sunxi_rsb_get_rtaddr(hwaddr);
  518. if (!rtaddr)
  519. continue;
  520. rdev = sunxi_rsb_device_create(rsb, child, hwaddr, rtaddr);
  521. if (IS_ERR(rdev))
  522. dev_err(dev, "failed to add child device %pOF: %ld\n",
  523. child, PTR_ERR(rdev));
  524. }
  525. return 0;
  526. }
  527. static int sunxi_rsb_hw_init(struct sunxi_rsb *rsb)
  528. {
  529. struct device *dev = rsb->dev;
  530. unsigned long p_clk_freq;
  531. u32 clk_delay, reg;
  532. int clk_div, ret;
  533. ret = clk_prepare_enable(rsb->clk);
  534. if (ret) {
  535. dev_err(dev, "failed to enable clk: %d\n", ret);
  536. return ret;
  537. }
  538. ret = reset_control_deassert(rsb->rstc);
  539. if (ret) {
  540. dev_err(dev, "failed to deassert reset line: %d\n", ret);
  541. goto err_clk_disable;
  542. }
  543. /* reset the controller */
  544. writel(RSB_CTRL_SOFT_RST, rsb->regs + RSB_CTRL);
  545. readl_poll_timeout(rsb->regs + RSB_CTRL, reg,
  546. !(reg & RSB_CTRL_SOFT_RST), 1000, 100000);
  547. /*
  548. * Clock frequency and delay calculation code is from
  549. * Allwinner U-boot sources.
  550. *
  551. * From A83 user manual:
  552. * bus clock frequency = parent clock frequency / (2 * (divider + 1))
  553. */
  554. p_clk_freq = clk_get_rate(rsb->clk);
  555. clk_div = p_clk_freq / rsb->clk_freq / 2;
  556. if (!clk_div)
  557. clk_div = 1;
  558. else if (clk_div > RSB_CCR_MAX_CLK_DIV + 1)
  559. clk_div = RSB_CCR_MAX_CLK_DIV + 1;
  560. clk_delay = clk_div >> 1;
  561. if (!clk_delay)
  562. clk_delay = 1;
  563. dev_info(dev, "RSB running at %lu Hz\n", p_clk_freq / clk_div / 2);
  564. writel(RSB_CCR_SDA_OUT_DELAY(clk_delay) | RSB_CCR_CLK_DIV(clk_div - 1),
  565. rsb->regs + RSB_CCR);
  566. return 0;
  567. err_clk_disable:
  568. clk_disable_unprepare(rsb->clk);
  569. return ret;
  570. }
  571. static void sunxi_rsb_hw_exit(struct sunxi_rsb *rsb)
  572. {
  573. reset_control_assert(rsb->rstc);
  574. /* Keep the clock and PM reference counts consistent. */
  575. if (!pm_runtime_status_suspended(rsb->dev))
  576. clk_disable_unprepare(rsb->clk);
  577. }
  578. static int __maybe_unused sunxi_rsb_runtime_suspend(struct device *dev)
  579. {
  580. struct sunxi_rsb *rsb = dev_get_drvdata(dev);
  581. clk_disable_unprepare(rsb->clk);
  582. return 0;
  583. }
  584. static int __maybe_unused sunxi_rsb_runtime_resume(struct device *dev)
  585. {
  586. struct sunxi_rsb *rsb = dev_get_drvdata(dev);
  587. return clk_prepare_enable(rsb->clk);
  588. }
  589. static int __maybe_unused sunxi_rsb_suspend(struct device *dev)
  590. {
  591. struct sunxi_rsb *rsb = dev_get_drvdata(dev);
  592. sunxi_rsb_hw_exit(rsb);
  593. return 0;
  594. }
  595. static int __maybe_unused sunxi_rsb_resume(struct device *dev)
  596. {
  597. struct sunxi_rsb *rsb = dev_get_drvdata(dev);
  598. return sunxi_rsb_hw_init(rsb);
  599. }
  600. static int sunxi_rsb_probe(struct platform_device *pdev)
  601. {
  602. struct device *dev = &pdev->dev;
  603. struct device_node *np = dev->of_node;
  604. struct sunxi_rsb *rsb;
  605. u32 clk_freq = 3000000;
  606. int irq, ret;
  607. of_property_read_u32(np, "clock-frequency", &clk_freq);
  608. if (clk_freq > RSB_MAX_FREQ)
  609. return dev_err_probe(dev, -EINVAL,
  610. "clock-frequency (%u Hz) is too high (max = 20MHz)\n",
  611. clk_freq);
  612. rsb = devm_kzalloc(dev, sizeof(*rsb), GFP_KERNEL);
  613. if (!rsb)
  614. return -ENOMEM;
  615. rsb->dev = dev;
  616. rsb->clk_freq = clk_freq;
  617. platform_set_drvdata(pdev, rsb);
  618. rsb->regs = devm_platform_ioremap_resource(pdev, 0);
  619. if (IS_ERR(rsb->regs))
  620. return PTR_ERR(rsb->regs);
  621. irq = platform_get_irq(pdev, 0);
  622. if (irq < 0)
  623. return irq;
  624. rsb->clk = devm_clk_get(dev, NULL);
  625. if (IS_ERR(rsb->clk))
  626. return dev_err_probe(dev, PTR_ERR(rsb->clk),
  627. "failed to retrieve clk\n");
  628. rsb->rstc = devm_reset_control_get(dev, NULL);
  629. if (IS_ERR(rsb->rstc))
  630. return dev_err_probe(dev, PTR_ERR(rsb->rstc),
  631. "failed to retrieve reset controller\n");
  632. init_completion(&rsb->complete);
  633. mutex_init(&rsb->lock);
  634. ret = devm_request_irq(dev, irq, sunxi_rsb_irq, 0, RSB_CTRL_NAME, rsb);
  635. if (ret)
  636. return dev_err_probe(dev, ret,
  637. "can't register interrupt handler irq %d\n", irq);
  638. ret = sunxi_rsb_hw_init(rsb);
  639. if (ret)
  640. return ret;
  641. /* initialize all devices on the bus into RSB mode */
  642. ret = sunxi_rsb_init_device_mode(rsb);
  643. if (ret)
  644. dev_warn(dev, "Initialize device mode failed: %d\n", ret);
  645. pm_suspend_ignore_children(dev, true);
  646. pm_runtime_set_active(dev);
  647. pm_runtime_set_autosuspend_delay(dev, MSEC_PER_SEC);
  648. pm_runtime_use_autosuspend(dev);
  649. pm_runtime_enable(dev);
  650. of_rsb_register_devices(rsb);
  651. return 0;
  652. }
  653. static void sunxi_rsb_remove(struct platform_device *pdev)
  654. {
  655. struct sunxi_rsb *rsb = platform_get_drvdata(pdev);
  656. device_for_each_child(rsb->dev, NULL, sunxi_rsb_remove_devices);
  657. pm_runtime_disable(&pdev->dev);
  658. sunxi_rsb_hw_exit(rsb);
  659. }
  660. static const struct dev_pm_ops sunxi_rsb_dev_pm_ops = {
  661. SET_RUNTIME_PM_OPS(sunxi_rsb_runtime_suspend,
  662. sunxi_rsb_runtime_resume, NULL)
  663. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(sunxi_rsb_suspend, sunxi_rsb_resume)
  664. };
  665. static const struct of_device_id sunxi_rsb_of_match_table[] = {
  666. { .compatible = "allwinner,sun8i-a23-rsb" },
  667. {}
  668. };
  669. MODULE_DEVICE_TABLE(of, sunxi_rsb_of_match_table);
  670. static struct platform_driver sunxi_rsb_driver = {
  671. .probe = sunxi_rsb_probe,
  672. .remove_new = sunxi_rsb_remove,
  673. .driver = {
  674. .name = RSB_CTRL_NAME,
  675. .of_match_table = sunxi_rsb_of_match_table,
  676. .pm = &sunxi_rsb_dev_pm_ops,
  677. },
  678. };
  679. static int __init sunxi_rsb_init(void)
  680. {
  681. int ret;
  682. ret = bus_register(&sunxi_rsb_bus);
  683. if (ret) {
  684. pr_err("failed to register sunxi sunxi_rsb bus: %d\n", ret);
  685. return ret;
  686. }
  687. ret = platform_driver_register(&sunxi_rsb_driver);
  688. if (ret) {
  689. bus_unregister(&sunxi_rsb_bus);
  690. return ret;
  691. }
  692. return 0;
  693. }
  694. module_init(sunxi_rsb_init);
  695. static void __exit sunxi_rsb_exit(void)
  696. {
  697. platform_driver_unregister(&sunxi_rsb_driver);
  698. bus_unregister(&sunxi_rsb_bus);
  699. }
  700. module_exit(sunxi_rsb_exit);
  701. MODULE_AUTHOR("Chen-Yu Tsai <wens@csie.org>");
  702. MODULE_DESCRIPTION("Allwinner sunXi Reduced Serial Bus controller driver");
  703. MODULE_LICENSE("GPL v2");