pinctrl-ti-iodelay.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. /*
  2. * Support for configuration of IO Delay module found on Texas Instruments SoCs
  3. * such as DRA7
  4. *
  5. * Copyright (C) 2015-2017 Texas Instruments Incorporated - http://www.ti.com/
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/of_device.h>
  17. #include <linux/pinctrl/pinconf.h>
  18. #include <linux/pinctrl/pinconf-generic.h>
  19. #include <linux/pinctrl/pinctrl.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #include "../core.h"
  23. #include "../devicetree.h"
  24. #define DRIVER_NAME "ti-iodelay"
  25. /**
  26. * struct ti_iodelay_reg_data - Describes the registers for the iodelay instance
  27. * @signature_mask: CONFIG_REG mask for the signature bits (see TRM)
  28. * @signature_value: CONFIG_REG signature value to be written (see TRM)
  29. * @lock_mask: CONFIG_REG mask for the lock bits (see TRM)
  30. * @lock_val: CONFIG_REG lock value for the lock bits (see TRM)
  31. * @unlock_val:CONFIG_REG unlock value for the lock bits (see TRM)
  32. * @binary_data_coarse_mask: CONFIG_REG coarse mask (see TRM)
  33. * @binary_data_fine_mask: CONFIG_REG fine mask (see TRM)
  34. * @reg_refclk_offset: Refclk register offset
  35. * @refclk_period_mask: Refclk mask
  36. * @reg_coarse_offset: Coarse register configuration offset
  37. * @coarse_delay_count_mask: Coarse delay count mask
  38. * @coarse_ref_count_mask: Coarse ref count mask
  39. * @reg_fine_offset: Fine register configuration offset
  40. * @fine_delay_count_mask: Fine delay count mask
  41. * @fine_ref_count_mask: Fine ref count mask
  42. * @reg_global_lock_offset: Global iodelay module lock register offset
  43. * @global_lock_mask: Lock mask
  44. * @global_unlock_val: Unlock value
  45. * @global_lock_val: Lock value
  46. * @reg_start_offset: Offset to iodelay registers after the CONFIG_REG_0 to 8
  47. * @reg_nr_per_pin: Number of iodelay registers for each pin
  48. * @regmap_config: Regmap configuration for the IODelay region
  49. */
  50. struct ti_iodelay_reg_data {
  51. u32 signature_mask;
  52. u32 signature_value;
  53. u32 lock_mask;
  54. u32 lock_val;
  55. u32 unlock_val;
  56. u32 binary_data_coarse_mask;
  57. u32 binary_data_fine_mask;
  58. u32 reg_refclk_offset;
  59. u32 refclk_period_mask;
  60. u32 reg_coarse_offset;
  61. u32 coarse_delay_count_mask;
  62. u32 coarse_ref_count_mask;
  63. u32 reg_fine_offset;
  64. u32 fine_delay_count_mask;
  65. u32 fine_ref_count_mask;
  66. u32 reg_global_lock_offset;
  67. u32 global_lock_mask;
  68. u32 global_unlock_val;
  69. u32 global_lock_val;
  70. u32 reg_start_offset;
  71. u32 reg_nr_per_pin;
  72. struct regmap_config *regmap_config;
  73. };
  74. /**
  75. * struct ti_iodelay_reg_values - Computed io_reg configuration values (see TRM)
  76. * @coarse_ref_count: Coarse reference count
  77. * @coarse_delay_count: Coarse delay count
  78. * @fine_ref_count: Fine reference count
  79. * @fine_delay_count: Fine Delay count
  80. * @ref_clk_period: Reference Clock period
  81. * @cdpe: Coarse delay parameter
  82. * @fdpe: Fine delay parameter
  83. */
  84. struct ti_iodelay_reg_values {
  85. u16 coarse_ref_count;
  86. u16 coarse_delay_count;
  87. u16 fine_ref_count;
  88. u16 fine_delay_count;
  89. u16 ref_clk_period;
  90. u32 cdpe;
  91. u32 fdpe;
  92. };
  93. /**
  94. * struct ti_iodelay_cfg - Description of each configuration parameters
  95. * @offset: Configuration register offset
  96. * @a_delay: Agnostic Delay (in ps)
  97. * @g_delay: Gnostic Delay (in ps)
  98. */
  99. struct ti_iodelay_cfg {
  100. u16 offset;
  101. u16 a_delay;
  102. u16 g_delay;
  103. };
  104. /**
  105. * struct ti_iodelay_pingroup - Structure that describes one group
  106. * @cfg: configuration array for the pin (from dt)
  107. * @ncfg: number of configuration values allocated
  108. * @config: pinconf "Config" - currently a dummy value
  109. */
  110. struct ti_iodelay_pingroup {
  111. struct ti_iodelay_cfg *cfg;
  112. int ncfg;
  113. unsigned long config;
  114. };
  115. /**
  116. * struct ti_iodelay_device - Represents information for a iodelay instance
  117. * @dev: Device pointer
  118. * @phys_base: Physical address base of the iodelay device
  119. * @reg_base: Virtual address base of the iodelay device
  120. * @regmap: Regmap for this iodelay instance
  121. * @pctl: Pinctrl device
  122. * @desc: pinctrl descriptor for pctl
  123. * @pa: pinctrl pin wise description
  124. * @reg_data: Register definition data for the IODelay instance
  125. * @reg_init_conf_values: Initial configuration values.
  126. */
  127. struct ti_iodelay_device {
  128. struct device *dev;
  129. unsigned long phys_base;
  130. void __iomem *reg_base;
  131. struct regmap *regmap;
  132. struct pinctrl_dev *pctl;
  133. struct pinctrl_desc desc;
  134. struct pinctrl_pin_desc *pa;
  135. const struct ti_iodelay_reg_data *reg_data;
  136. struct ti_iodelay_reg_values reg_init_conf_values;
  137. };
  138. /**
  139. * ti_iodelay_extract() - extract bits for a field
  140. * @val: Register value
  141. * @mask: Mask
  142. *
  143. * Return: extracted value which is appropriately shifted
  144. */
  145. static inline u32 ti_iodelay_extract(u32 val, u32 mask)
  146. {
  147. return (val & mask) >> __ffs(mask);
  148. }
  149. /**
  150. * ti_iodelay_compute_dpe() - Compute equation for delay parameter
  151. * @period: Period to use
  152. * @ref: Reference Count
  153. * @delay: Delay count
  154. * @delay_m: Delay multiplier
  155. *
  156. * Return: Computed delay parameter
  157. */
  158. static inline u32 ti_iodelay_compute_dpe(u16 period, u16 ref, u16 delay,
  159. u16 delay_m)
  160. {
  161. u64 m, d;
  162. /* Handle overflow conditions */
  163. m = 10 * (u64)period * (u64)ref;
  164. d = 2 * (u64)delay * (u64)delay_m;
  165. /* Truncate result back to 32 bits */
  166. return div64_u64(m, d);
  167. }
  168. /**
  169. * ti_iodelay_pinconf_set() - Configure the pin configuration
  170. * @iod: iodelay device
  171. * @cfg: Configuration value
  172. *
  173. * Update the configuration register as per TRM and lockup once done.
  174. * *IMPORTANT NOTE* SoC TRM does recommend doing iodelay programmation only
  175. * while in Isolation. But, then, isolation also implies that every pin
  176. * on the SoC (including DDR) will be isolated out. The only benefit being
  177. * a glitchless configuration, However, the intent of this driver is purely
  178. * to support a "glitchy" configuration where applicable.
  179. *
  180. * Return: 0 in case of success, else appropriate error value
  181. */
  182. static int ti_iodelay_pinconf_set(struct ti_iodelay_device *iod,
  183. struct ti_iodelay_cfg *cfg)
  184. {
  185. const struct ti_iodelay_reg_data *reg = iod->reg_data;
  186. struct ti_iodelay_reg_values *ival = &iod->reg_init_conf_values;
  187. struct device *dev = iod->dev;
  188. u32 g_delay_coarse, g_delay_fine;
  189. u32 a_delay_coarse, a_delay_fine;
  190. u32 c_elements, f_elements;
  191. u32 total_delay;
  192. u32 reg_mask, reg_val, tmp_val;
  193. int r;
  194. /* NOTE: Truncation is expected in all division below */
  195. g_delay_coarse = cfg->g_delay / 920;
  196. g_delay_fine = ((cfg->g_delay % 920) * 10) / 60;
  197. a_delay_coarse = cfg->a_delay / ival->cdpe;
  198. a_delay_fine = ((cfg->a_delay % ival->cdpe) * 10) / ival->fdpe;
  199. c_elements = g_delay_coarse + a_delay_coarse;
  200. f_elements = (g_delay_fine + a_delay_fine) / 10;
  201. if (f_elements > 22) {
  202. total_delay = c_elements * ival->cdpe + f_elements * ival->fdpe;
  203. c_elements = total_delay / ival->cdpe;
  204. f_elements = (total_delay % ival->cdpe) / ival->fdpe;
  205. }
  206. reg_mask = reg->signature_mask;
  207. reg_val = reg->signature_value << __ffs(reg->signature_mask);
  208. reg_mask |= reg->binary_data_coarse_mask;
  209. tmp_val = c_elements << __ffs(reg->binary_data_coarse_mask);
  210. if (tmp_val & ~reg->binary_data_coarse_mask) {
  211. dev_err(dev, "Masking overflow of coarse elements %08x\n",
  212. tmp_val);
  213. tmp_val &= reg->binary_data_coarse_mask;
  214. }
  215. reg_val |= tmp_val;
  216. reg_mask |= reg->binary_data_fine_mask;
  217. tmp_val = f_elements << __ffs(reg->binary_data_fine_mask);
  218. if (tmp_val & ~reg->binary_data_fine_mask) {
  219. dev_err(dev, "Masking overflow of fine elements %08x\n",
  220. tmp_val);
  221. tmp_val &= reg->binary_data_fine_mask;
  222. }
  223. reg_val |= tmp_val;
  224. /*
  225. * NOTE: we leave the iodelay values unlocked - this is to work around
  226. * situations such as those found with mmc mode change.
  227. * However, this leaves open any unwarranted changes to padconf register
  228. * impacting iodelay configuration. Use with care!
  229. */
  230. reg_mask |= reg->lock_mask;
  231. reg_val |= reg->unlock_val << __ffs(reg->lock_mask);
  232. r = regmap_update_bits(iod->regmap, cfg->offset, reg_mask, reg_val);
  233. dev_dbg(dev, "Set reg 0x%x Delay(a: %d g: %d), Elements(C=%d F=%d)0x%x\n",
  234. cfg->offset, cfg->a_delay, cfg->g_delay, c_elements,
  235. f_elements, reg_val);
  236. return r;
  237. }
  238. /**
  239. * ti_iodelay_pinconf_init_dev() - Initialize IODelay device
  240. * @iod: iodelay device
  241. *
  242. * Unlocks the iodelay region, computes the common parameters
  243. *
  244. * Return: 0 in case of success, else appropriate error value
  245. */
  246. static int ti_iodelay_pinconf_init_dev(struct ti_iodelay_device *iod)
  247. {
  248. const struct ti_iodelay_reg_data *reg = iod->reg_data;
  249. struct device *dev = iod->dev;
  250. struct ti_iodelay_reg_values *ival = &iod->reg_init_conf_values;
  251. u32 val;
  252. int r;
  253. /* unlock the iodelay region */
  254. r = regmap_update_bits(iod->regmap, reg->reg_global_lock_offset,
  255. reg->global_lock_mask, reg->global_unlock_val);
  256. if (r)
  257. return r;
  258. /* Read up Recalibration sequence done by bootloader */
  259. r = regmap_read(iod->regmap, reg->reg_refclk_offset, &val);
  260. if (r)
  261. return r;
  262. ival->ref_clk_period = ti_iodelay_extract(val, reg->refclk_period_mask);
  263. dev_dbg(dev, "refclk_period=0x%04x\n", ival->ref_clk_period);
  264. r = regmap_read(iod->regmap, reg->reg_coarse_offset, &val);
  265. if (r)
  266. return r;
  267. ival->coarse_ref_count =
  268. ti_iodelay_extract(val, reg->coarse_ref_count_mask);
  269. ival->coarse_delay_count =
  270. ti_iodelay_extract(val, reg->coarse_delay_count_mask);
  271. if (!ival->coarse_delay_count) {
  272. dev_err(dev, "Invalid Coarse delay count (0) (reg=0x%08x)\n",
  273. val);
  274. return -EINVAL;
  275. }
  276. ival->cdpe = ti_iodelay_compute_dpe(ival->ref_clk_period,
  277. ival->coarse_ref_count,
  278. ival->coarse_delay_count, 88);
  279. if (!ival->cdpe) {
  280. dev_err(dev, "Invalid cdpe computed params = %d %d %d\n",
  281. ival->ref_clk_period, ival->coarse_ref_count,
  282. ival->coarse_delay_count);
  283. return -EINVAL;
  284. }
  285. dev_dbg(iod->dev, "coarse: ref=0x%04x delay=0x%04x cdpe=0x%08x\n",
  286. ival->coarse_ref_count, ival->coarse_delay_count, ival->cdpe);
  287. r = regmap_read(iod->regmap, reg->reg_fine_offset, &val);
  288. if (r)
  289. return r;
  290. ival->fine_ref_count =
  291. ti_iodelay_extract(val, reg->fine_ref_count_mask);
  292. ival->fine_delay_count =
  293. ti_iodelay_extract(val, reg->fine_delay_count_mask);
  294. if (!ival->fine_delay_count) {
  295. dev_err(dev, "Invalid Fine delay count (0) (reg=0x%08x)\n",
  296. val);
  297. return -EINVAL;
  298. }
  299. ival->fdpe = ti_iodelay_compute_dpe(ival->ref_clk_period,
  300. ival->fine_ref_count,
  301. ival->fine_delay_count, 264);
  302. if (!ival->fdpe) {
  303. dev_err(dev, "Invalid fdpe(0) computed params = %d %d %d\n",
  304. ival->ref_clk_period, ival->fine_ref_count,
  305. ival->fine_delay_count);
  306. return -EINVAL;
  307. }
  308. dev_dbg(iod->dev, "fine: ref=0x%04x delay=0x%04x fdpe=0x%08x\n",
  309. ival->fine_ref_count, ival->fine_delay_count, ival->fdpe);
  310. return 0;
  311. }
  312. /**
  313. * ti_iodelay_pinconf_deinit_dev() - deinit the iodelay device
  314. * @iod: IODelay device
  315. *
  316. * Deinitialize the IODelay device (basically just lock the region back up.
  317. */
  318. static void ti_iodelay_pinconf_deinit_dev(struct ti_iodelay_device *iod)
  319. {
  320. const struct ti_iodelay_reg_data *reg = iod->reg_data;
  321. /* lock the iodelay region back again */
  322. regmap_update_bits(iod->regmap, reg->reg_global_lock_offset,
  323. reg->global_lock_mask, reg->global_lock_val);
  324. }
  325. /**
  326. * ti_iodelay_get_pingroup() - Find the group mapped by a group selector
  327. * @iod: iodelay device
  328. * @selector: Group Selector
  329. *
  330. * Return: Corresponding group representing group selector
  331. */
  332. static struct ti_iodelay_pingroup *
  333. ti_iodelay_get_pingroup(struct ti_iodelay_device *iod, unsigned int selector)
  334. {
  335. struct group_desc *g;
  336. g = pinctrl_generic_get_group(iod->pctl, selector);
  337. if (!g) {
  338. dev_err(iod->dev, "%s could not find pingroup %i\n", __func__,
  339. selector);
  340. return NULL;
  341. }
  342. return g->data;
  343. }
  344. /**
  345. * ti_iodelay_offset_to_pin() - get a pin index based on the register offset
  346. * @iod: iodelay driver instance
  347. * @offset: register offset from the base
  348. */
  349. static int ti_iodelay_offset_to_pin(struct ti_iodelay_device *iod,
  350. unsigned int offset)
  351. {
  352. const struct ti_iodelay_reg_data *r = iod->reg_data;
  353. unsigned int index;
  354. if (offset > r->regmap_config->max_register) {
  355. dev_err(iod->dev, "mux offset out of range: 0x%x (0x%x)\n",
  356. offset, r->regmap_config->max_register);
  357. return -EINVAL;
  358. }
  359. index = (offset - r->reg_start_offset) / r->regmap_config->reg_stride;
  360. index /= r->reg_nr_per_pin;
  361. return index;
  362. }
  363. /**
  364. * ti_iodelay_node_iterator() - Iterate iodelay node
  365. * @pctldev: Pin controller driver
  366. * @np: Device node
  367. * @pinctrl_spec: Parsed arguments from device tree
  368. * @pins: Array of pins in the pin group
  369. * @pin_index: Pin index in the pin array
  370. * @data: Pin controller driver specific data
  371. *
  372. */
  373. static int ti_iodelay_node_iterator(struct pinctrl_dev *pctldev,
  374. struct device_node *np,
  375. const struct of_phandle_args *pinctrl_spec,
  376. int *pins, int pin_index, void *data)
  377. {
  378. struct ti_iodelay_device *iod;
  379. struct ti_iodelay_cfg *cfg = data;
  380. const struct ti_iodelay_reg_data *r;
  381. struct pinctrl_pin_desc *pd;
  382. int pin;
  383. iod = pinctrl_dev_get_drvdata(pctldev);
  384. if (!iod)
  385. return -EINVAL;
  386. r = iod->reg_data;
  387. if (pinctrl_spec->args_count < r->reg_nr_per_pin) {
  388. dev_err(iod->dev, "invalid args_count for spec: %i\n",
  389. pinctrl_spec->args_count);
  390. return -EINVAL;
  391. }
  392. /* Index plus two value cells */
  393. cfg[pin_index].offset = pinctrl_spec->args[0];
  394. cfg[pin_index].a_delay = pinctrl_spec->args[1] & 0xffff;
  395. cfg[pin_index].g_delay = pinctrl_spec->args[2] & 0xffff;
  396. pin = ti_iodelay_offset_to_pin(iod, cfg[pin_index].offset);
  397. if (pin < 0) {
  398. dev_err(iod->dev, "could not add functions for %s %ux\n",
  399. np->name, cfg[pin_index].offset);
  400. return -ENODEV;
  401. }
  402. pins[pin_index] = pin;
  403. pd = &iod->pa[pin];
  404. pd->drv_data = &cfg[pin_index];
  405. dev_dbg(iod->dev, "%s offset=%x a_delay = %d g_delay = %d\n",
  406. np->name, cfg[pin_index].offset, cfg[pin_index].a_delay,
  407. cfg[pin_index].g_delay);
  408. return 0;
  409. }
  410. /**
  411. * ti_iodelay_dt_node_to_map() - Map a device tree node to appropriate group
  412. * @pctldev: pinctrl device representing IODelay device
  413. * @np: Node Pointer (device tree)
  414. * @map: Pinctrl Map returned back to pinctrl framework
  415. * @num_maps: Number of maps (1)
  416. *
  417. * Maps the device tree description into a group of configuration parameters
  418. * for iodelay block entry.
  419. *
  420. * Return: 0 in case of success, else appropriate error value
  421. */
  422. static int ti_iodelay_dt_node_to_map(struct pinctrl_dev *pctldev,
  423. struct device_node *np,
  424. struct pinctrl_map **map,
  425. unsigned int *num_maps)
  426. {
  427. struct ti_iodelay_device *iod;
  428. struct ti_iodelay_cfg *cfg;
  429. struct ti_iodelay_pingroup *g;
  430. const char *name = "pinctrl-pin-array";
  431. int rows, *pins, error = -EINVAL, i;
  432. iod = pinctrl_dev_get_drvdata(pctldev);
  433. if (!iod)
  434. return -EINVAL;
  435. rows = pinctrl_count_index_with_args(np, name);
  436. if (rows == -EINVAL)
  437. return rows;
  438. *map = devm_kzalloc(iod->dev, sizeof(**map), GFP_KERNEL);
  439. if (!*map)
  440. return -ENOMEM;
  441. *num_maps = 0;
  442. g = devm_kzalloc(iod->dev, sizeof(*g), GFP_KERNEL);
  443. if (!g) {
  444. error = -ENOMEM;
  445. goto free_map;
  446. }
  447. pins = devm_kzalloc(iod->dev, sizeof(*pins) * rows, GFP_KERNEL);
  448. if (!pins)
  449. goto free_group;
  450. cfg = devm_kzalloc(iod->dev, sizeof(*cfg) * rows, GFP_KERNEL);
  451. if (!cfg) {
  452. error = -ENOMEM;
  453. goto free_pins;
  454. }
  455. for (i = 0; i < rows; i++) {
  456. struct of_phandle_args pinctrl_spec;
  457. error = pinctrl_parse_index_with_args(np, name, i,
  458. &pinctrl_spec);
  459. if (error)
  460. goto free_data;
  461. error = ti_iodelay_node_iterator(pctldev, np, &pinctrl_spec,
  462. pins, i, cfg);
  463. if (error)
  464. goto free_data;
  465. }
  466. g->cfg = cfg;
  467. g->ncfg = i;
  468. g->config = PIN_CONFIG_END;
  469. error = pinctrl_generic_add_group(iod->pctl, np->name, pins, i, g);
  470. if (error < 0)
  471. goto free_data;
  472. (*map)->type = PIN_MAP_TYPE_CONFIGS_GROUP;
  473. (*map)->data.configs.group_or_pin = np->name;
  474. (*map)->data.configs.configs = &g->config;
  475. (*map)->data.configs.num_configs = 1;
  476. *num_maps = 1;
  477. return 0;
  478. free_data:
  479. devm_kfree(iod->dev, cfg);
  480. free_pins:
  481. devm_kfree(iod->dev, pins);
  482. free_group:
  483. devm_kfree(iod->dev, g);
  484. free_map:
  485. devm_kfree(iod->dev, *map);
  486. return error;
  487. }
  488. /**
  489. * ti_iodelay_pinconf_group_get() - Get the group configuration
  490. * @pctldev: pinctrl device representing IODelay device
  491. * @selector: Group selector
  492. * @config: Configuration returned
  493. *
  494. * Return: The configuration if the group is valid, else returns -EINVAL
  495. */
  496. static int ti_iodelay_pinconf_group_get(struct pinctrl_dev *pctldev,
  497. unsigned int selector,
  498. unsigned long *config)
  499. {
  500. struct ti_iodelay_device *iod;
  501. struct device *dev;
  502. struct ti_iodelay_pingroup *group;
  503. iod = pinctrl_dev_get_drvdata(pctldev);
  504. dev = iod->dev;
  505. group = ti_iodelay_get_pingroup(iod, selector);
  506. if (!group)
  507. return -EINVAL;
  508. *config = group->config;
  509. return 0;
  510. }
  511. /**
  512. * ti_iodelay_pinconf_group_set() - Configure the groups of pins
  513. * @pctldev: pinctrl device representing IODelay device
  514. * @selector: Group selector
  515. * @configs: Configurations
  516. * @num_configs: Number of configurations
  517. *
  518. * Return: 0 if all went fine, else appropriate error value.
  519. */
  520. static int ti_iodelay_pinconf_group_set(struct pinctrl_dev *pctldev,
  521. unsigned int selector,
  522. unsigned long *configs,
  523. unsigned int num_configs)
  524. {
  525. struct ti_iodelay_device *iod;
  526. struct device *dev;
  527. struct ti_iodelay_pingroup *group;
  528. int i;
  529. iod = pinctrl_dev_get_drvdata(pctldev);
  530. dev = iod->dev;
  531. group = ti_iodelay_get_pingroup(iod, selector);
  532. if (num_configs != 1) {
  533. dev_err(dev, "Unsupported number of configurations %d\n",
  534. num_configs);
  535. return -EINVAL;
  536. }
  537. if (*configs != PIN_CONFIG_END) {
  538. dev_err(dev, "Unsupported configuration\n");
  539. return -EINVAL;
  540. }
  541. for (i = 0; i < group->ncfg; i++) {
  542. if (ti_iodelay_pinconf_set(iod, &group->cfg[i]))
  543. return -ENOTSUPP;
  544. }
  545. return 0;
  546. }
  547. #ifdef CONFIG_DEBUG_FS
  548. /**
  549. * ti_iodelay_pin_to_offset() - get pin register offset based on the pin index
  550. * @iod: iodelay driver instance
  551. * @selector: Pin index
  552. */
  553. static unsigned int ti_iodelay_pin_to_offset(struct ti_iodelay_device *iod,
  554. unsigned int selector)
  555. {
  556. const struct ti_iodelay_reg_data *r = iod->reg_data;
  557. unsigned int offset;
  558. offset = selector * r->regmap_config->reg_stride;
  559. offset *= r->reg_nr_per_pin;
  560. offset += r->reg_start_offset;
  561. return offset;
  562. }
  563. static void ti_iodelay_pin_dbg_show(struct pinctrl_dev *pctldev,
  564. struct seq_file *s,
  565. unsigned int pin)
  566. {
  567. struct ti_iodelay_device *iod;
  568. struct pinctrl_pin_desc *pd;
  569. struct ti_iodelay_cfg *cfg;
  570. const struct ti_iodelay_reg_data *r;
  571. unsigned long offset;
  572. u32 in, oen, out;
  573. iod = pinctrl_dev_get_drvdata(pctldev);
  574. r = iod->reg_data;
  575. offset = ti_iodelay_pin_to_offset(iod, pin);
  576. if (pin < 0) {
  577. dev_err(iod->dev, "invalid pin offset for pin%i\n", pin);
  578. return;
  579. }
  580. pd = &iod->pa[pin];
  581. cfg = pd->drv_data;
  582. regmap_read(iod->regmap, offset, &in);
  583. regmap_read(iod->regmap, offset + r->regmap_config->reg_stride, &oen);
  584. regmap_read(iod->regmap, offset + r->regmap_config->reg_stride * 2,
  585. &out);
  586. seq_printf(s, "%lx a: %i g: %i (%08x %08x %08x) %s ",
  587. iod->phys_base + offset,
  588. cfg ? cfg->a_delay : -1,
  589. cfg ? cfg->g_delay : -1,
  590. in, oen, out, DRIVER_NAME);
  591. }
  592. /**
  593. * ti_iodelay_pinconf_group_dbg_show() - show the group information
  594. * @pctldev: Show the group information
  595. * @s: Sequence file
  596. * @selector: Group selector
  597. *
  598. * Provide the configuration information of the selected group
  599. */
  600. static void ti_iodelay_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
  601. struct seq_file *s,
  602. unsigned int selector)
  603. {
  604. struct ti_iodelay_device *iod;
  605. struct device *dev;
  606. struct ti_iodelay_pingroup *group;
  607. int i;
  608. iod = pinctrl_dev_get_drvdata(pctldev);
  609. dev = iod->dev;
  610. group = ti_iodelay_get_pingroup(iod, selector);
  611. if (!group)
  612. return;
  613. for (i = 0; i < group->ncfg; i++) {
  614. struct ti_iodelay_cfg *cfg;
  615. u32 reg = 0;
  616. cfg = &group->cfg[i];
  617. regmap_read(iod->regmap, cfg->offset, &reg),
  618. seq_printf(s, "\n\t0x%08x = 0x%08x (%3d, %3d)",
  619. cfg->offset, reg, cfg->a_delay,
  620. cfg->g_delay);
  621. }
  622. }
  623. #endif
  624. static struct pinctrl_ops ti_iodelay_pinctrl_ops = {
  625. .get_groups_count = pinctrl_generic_get_group_count,
  626. .get_group_name = pinctrl_generic_get_group_name,
  627. .get_group_pins = pinctrl_generic_get_group_pins,
  628. #ifdef CONFIG_DEBUG_FS
  629. .pin_dbg_show = ti_iodelay_pin_dbg_show,
  630. #endif
  631. .dt_node_to_map = ti_iodelay_dt_node_to_map,
  632. };
  633. static struct pinconf_ops ti_iodelay_pinctrl_pinconf_ops = {
  634. .pin_config_group_get = ti_iodelay_pinconf_group_get,
  635. .pin_config_group_set = ti_iodelay_pinconf_group_set,
  636. #ifdef CONFIG_DEBUG_FS
  637. .pin_config_group_dbg_show = ti_iodelay_pinconf_group_dbg_show,
  638. #endif
  639. };
  640. /**
  641. * ti_iodelay_alloc_pins() - Allocate structures needed for pins for iodelay
  642. * @dev: Device pointer
  643. * @iod: iodelay device
  644. * @base_phy: Base Physical Address
  645. *
  646. * Return: 0 if all went fine, else appropriate error value.
  647. */
  648. static int ti_iodelay_alloc_pins(struct device *dev,
  649. struct ti_iodelay_device *iod, u32 base_phy)
  650. {
  651. const struct ti_iodelay_reg_data *r = iod->reg_data;
  652. struct pinctrl_pin_desc *pin;
  653. u32 phy_reg;
  654. int nr_pins, i;
  655. nr_pins = ti_iodelay_offset_to_pin(iod, r->regmap_config->max_register);
  656. dev_dbg(dev, "Allocating %i pins\n", nr_pins);
  657. iod->pa = devm_kzalloc(dev, sizeof(*iod->pa) * nr_pins, GFP_KERNEL);
  658. if (!iod->pa)
  659. return -ENOMEM;
  660. iod->desc.pins = iod->pa;
  661. iod->desc.npins = nr_pins;
  662. phy_reg = r->reg_start_offset + base_phy;
  663. for (i = 0; i < nr_pins; i++, phy_reg += 4) {
  664. pin = &iod->pa[i];
  665. pin->number = i;
  666. }
  667. return 0;
  668. }
  669. static struct regmap_config dra7_iodelay_regmap_config = {
  670. .reg_bits = 32,
  671. .reg_stride = 4,
  672. .val_bits = 32,
  673. .max_register = 0xd1c,
  674. };
  675. static struct ti_iodelay_reg_data dra7_iodelay_data = {
  676. .signature_mask = 0x0003f000,
  677. .signature_value = 0x29,
  678. .lock_mask = 0x00000400,
  679. .lock_val = 1,
  680. .unlock_val = 0,
  681. .binary_data_coarse_mask = 0x000003e0,
  682. .binary_data_fine_mask = 0x0000001f,
  683. .reg_refclk_offset = 0x14,
  684. .refclk_period_mask = 0xffff,
  685. .reg_coarse_offset = 0x18,
  686. .coarse_delay_count_mask = 0xffff0000,
  687. .coarse_ref_count_mask = 0x0000ffff,
  688. .reg_fine_offset = 0x1C,
  689. .fine_delay_count_mask = 0xffff0000,
  690. .fine_ref_count_mask = 0x0000ffff,
  691. .reg_global_lock_offset = 0x2c,
  692. .global_lock_mask = 0x0000ffff,
  693. .global_unlock_val = 0x0000aaaa,
  694. .global_lock_val = 0x0000aaab,
  695. .reg_start_offset = 0x30,
  696. .reg_nr_per_pin = 3,
  697. .regmap_config = &dra7_iodelay_regmap_config,
  698. };
  699. static const struct of_device_id ti_iodelay_of_match[] = {
  700. {.compatible = "ti,dra7-iodelay", .data = &dra7_iodelay_data},
  701. { /* Hopefully no more.. */ },
  702. };
  703. MODULE_DEVICE_TABLE(of, ti_iodelay_of_match);
  704. /**
  705. * ti_iodelay_probe() - Standard probe
  706. * @pdev: platform device
  707. *
  708. * Return: 0 if all went fine, else appropriate error value.
  709. */
  710. static int ti_iodelay_probe(struct platform_device *pdev)
  711. {
  712. struct device *dev = &pdev->dev;
  713. struct device_node *np = of_node_get(dev->of_node);
  714. const struct of_device_id *match;
  715. struct resource *res;
  716. struct ti_iodelay_device *iod;
  717. int ret = 0;
  718. if (!np) {
  719. ret = -EINVAL;
  720. dev_err(dev, "No OF node\n");
  721. goto exit_out;
  722. }
  723. match = of_match_device(ti_iodelay_of_match, dev);
  724. if (!match) {
  725. ret = -EINVAL;
  726. dev_err(dev, "No DATA match\n");
  727. goto exit_out;
  728. }
  729. iod = devm_kzalloc(dev, sizeof(*iod), GFP_KERNEL);
  730. if (!iod) {
  731. ret = -ENOMEM;
  732. goto exit_out;
  733. }
  734. iod->dev = dev;
  735. iod->reg_data = match->data;
  736. /* So far We can assume there is only 1 bank of registers */
  737. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  738. if (!res) {
  739. dev_err(dev, "Missing MEM resource\n");
  740. ret = -ENODEV;
  741. goto exit_out;
  742. }
  743. iod->phys_base = res->start;
  744. iod->reg_base = devm_ioremap_resource(dev, res);
  745. if (IS_ERR(iod->reg_base)) {
  746. ret = PTR_ERR(iod->reg_base);
  747. goto exit_out;
  748. }
  749. iod->regmap = devm_regmap_init_mmio(dev, iod->reg_base,
  750. iod->reg_data->regmap_config);
  751. if (IS_ERR(iod->regmap)) {
  752. dev_err(dev, "Regmap MMIO init failed.\n");
  753. ret = PTR_ERR(iod->regmap);
  754. goto exit_out;
  755. }
  756. if (ti_iodelay_pinconf_init_dev(iod))
  757. goto exit_out;
  758. ret = ti_iodelay_alloc_pins(dev, iod, res->start);
  759. if (ret)
  760. goto exit_out;
  761. iod->desc.pctlops = &ti_iodelay_pinctrl_ops;
  762. /* no pinmux ops - we are pinconf */
  763. iod->desc.confops = &ti_iodelay_pinctrl_pinconf_ops;
  764. iod->desc.name = dev_name(dev);
  765. iod->desc.owner = THIS_MODULE;
  766. ret = pinctrl_register_and_init(&iod->desc, dev, iod, &iod->pctl);
  767. if (ret) {
  768. dev_err(dev, "Failed to register pinctrl\n");
  769. goto exit_out;
  770. }
  771. platform_set_drvdata(pdev, iod);
  772. exit_out:
  773. of_node_put(np);
  774. return ret;
  775. }
  776. /**
  777. * ti_iodelay_remove() - standard remove
  778. * @pdev: platform device
  779. *
  780. * Return: 0 if all went fine, else appropriate error value.
  781. */
  782. static int ti_iodelay_remove(struct platform_device *pdev)
  783. {
  784. struct ti_iodelay_device *iod = platform_get_drvdata(pdev);
  785. if (!iod)
  786. return 0;
  787. if (iod->pctl)
  788. pinctrl_unregister(iod->pctl);
  789. ti_iodelay_pinconf_deinit_dev(iod);
  790. /* Expect other allocations to be freed by devm */
  791. return 0;
  792. }
  793. static struct platform_driver ti_iodelay_driver = {
  794. .probe = ti_iodelay_probe,
  795. .remove = ti_iodelay_remove,
  796. .driver = {
  797. .owner = THIS_MODULE,
  798. .name = DRIVER_NAME,
  799. .of_match_table = ti_iodelay_of_match,
  800. },
  801. };
  802. module_platform_driver(ti_iodelay_driver);
  803. MODULE_AUTHOR("Texas Instruments, Inc.");
  804. MODULE_DESCRIPTION("Pinconf driver for TI's IO Delay module");
  805. MODULE_LICENSE("GPL v2");