sunxi-rsb.c 19 KB

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