1ea9c2605SLucas Stach /* 2ea9c2605SLucas Stach * Copyright (c) 2016-2017 Lucas Stach, Pengutronix 3ea9c2605SLucas Stach * 4ea9c2605SLucas Stach * This program is free software; you can redistribute it and/or modify it 5ea9c2605SLucas Stach * under the terms and conditions of the GNU General Public License, 6ea9c2605SLucas Stach * version 2, as published by the Free Software Foundation. 7ea9c2605SLucas Stach * 8ea9c2605SLucas Stach * This program is distributed in the hope it will be useful, but WITHOUT 9ea9c2605SLucas Stach * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10ea9c2605SLucas Stach * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11ea9c2605SLucas Stach * more details. 12ea9c2605SLucas Stach */ 13ea9c2605SLucas Stach 14ea9c2605SLucas Stach #include <drm/drm_fourcc.h> 15ea9c2605SLucas Stach #include <linux/clk.h> 16ea9c2605SLucas Stach #include <linux/err.h> 17263c3b80SLucas Stach #include <linux/iopoll.h> 18ea9c2605SLucas Stach #include <linux/mfd/syscon.h> 19ea9c2605SLucas Stach #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h> 20ea9c2605SLucas Stach #include <linux/module.h> 21ea9c2605SLucas Stach #include <linux/of.h> 22ea9c2605SLucas Stach #include <linux/platform_device.h> 2372944089SLucas Stach #include <linux/pm_runtime.h> 24ea9c2605SLucas Stach #include <linux/regmap.h> 25ea9c2605SLucas Stach #include <video/imx-ipu-v3.h> 26ea9c2605SLucas Stach 27ea9c2605SLucas Stach #include "ipu-prv.h" 28ea9c2605SLucas Stach 29ea9c2605SLucas Stach #define IPU_PRG_CTL 0x00 30ea9c2605SLucas Stach #define IPU_PRG_CTL_BYPASS(i) (1 << (0 + i)) 31ea9c2605SLucas Stach #define IPU_PRG_CTL_SOFT_ARID_MASK 0x3 32ea9c2605SLucas Stach #define IPU_PRG_CTL_SOFT_ARID_SHIFT(i) (8 + i * 2) 33ea9c2605SLucas Stach #define IPU_PRG_CTL_SOFT_ARID(i, v) ((v & 0x3) << (8 + 2 * i)) 34ea9c2605SLucas Stach #define IPU_PRG_CTL_SO(i) (1 << (16 + i)) 35ea9c2605SLucas Stach #define IPU_PRG_CTL_VFLIP(i) (1 << (19 + i)) 36ea9c2605SLucas Stach #define IPU_PRG_CTL_BLOCK_MODE(i) (1 << (22 + i)) 37ea9c2605SLucas Stach #define IPU_PRG_CTL_CNT_LOAD_EN(i) (1 << (25 + i)) 38ea9c2605SLucas Stach #define IPU_PRG_CTL_SOFTRST (1 << 30) 39ea9c2605SLucas Stach #define IPU_PRG_CTL_SHADOW_EN (1 << 31) 40ea9c2605SLucas Stach 41ea9c2605SLucas Stach #define IPU_PRG_STATUS 0x04 42ea9c2605SLucas Stach #define IPU_PRG_STATUS_BUFFER0_READY(i) (1 << (0 + i * 2)) 43ea9c2605SLucas Stach #define IPU_PRG_STATUS_BUFFER1_READY(i) (1 << (1 + i * 2)) 44ea9c2605SLucas Stach 45ea9c2605SLucas Stach #define IPU_PRG_QOS 0x08 46ea9c2605SLucas Stach #define IPU_PRG_QOS_ARID_MASK 0xf 47ea9c2605SLucas Stach #define IPU_PRG_QOS_ARID_SHIFT(i) (0 + i * 4) 48ea9c2605SLucas Stach 49ea9c2605SLucas Stach #define IPU_PRG_REG_UPDATE 0x0c 50ea9c2605SLucas Stach #define IPU_PRG_REG_UPDATE_REG_UPDATE (1 << 0) 51ea9c2605SLucas Stach 52ea9c2605SLucas Stach #define IPU_PRG_STRIDE(i) (0x10 + i * 0x4) 53ea9c2605SLucas Stach #define IPU_PRG_STRIDE_STRIDE_MASK 0x3fff 54ea9c2605SLucas Stach 55ea9c2605SLucas Stach #define IPU_PRG_CROP_LINE 0x1c 56ea9c2605SLucas Stach 57ea9c2605SLucas Stach #define IPU_PRG_THD 0x20 58ea9c2605SLucas Stach 59ea9c2605SLucas Stach #define IPU_PRG_BADDR(i) (0x24 + i * 0x4) 60ea9c2605SLucas Stach 61ea9c2605SLucas Stach #define IPU_PRG_OFFSET(i) (0x30 + i * 0x4) 62ea9c2605SLucas Stach 63ea9c2605SLucas Stach #define IPU_PRG_ILO(i) (0x3c + i * 0x4) 64ea9c2605SLucas Stach 65ea9c2605SLucas Stach #define IPU_PRG_HEIGHT(i) (0x48 + i * 0x4) 66ea9c2605SLucas Stach #define IPU_PRG_HEIGHT_PRE_HEIGHT_MASK 0xfff 67ea9c2605SLucas Stach #define IPU_PRG_HEIGHT_PRE_HEIGHT_SHIFT 0 68ea9c2605SLucas Stach #define IPU_PRG_HEIGHT_IPU_HEIGHT_MASK 0xfff 69ea9c2605SLucas Stach #define IPU_PRG_HEIGHT_IPU_HEIGHT_SHIFT 16 70ea9c2605SLucas Stach 71ea9c2605SLucas Stach struct ipu_prg_channel { 72ea9c2605SLucas Stach bool enabled; 73ea9c2605SLucas Stach int used_pre; 74ea9c2605SLucas Stach }; 75ea9c2605SLucas Stach 76ea9c2605SLucas Stach struct ipu_prg { 77ea9c2605SLucas Stach struct list_head list; 78ea9c2605SLucas Stach struct device *dev; 79ea9c2605SLucas Stach int id; 80ea9c2605SLucas Stach 81ea9c2605SLucas Stach void __iomem *regs; 82ea9c2605SLucas Stach struct clk *clk_ipg, *clk_axi; 83ea9c2605SLucas Stach struct regmap *iomuxc_gpr; 84ea9c2605SLucas Stach struct ipu_pre *pres[3]; 85ea9c2605SLucas Stach 86ea9c2605SLucas Stach struct ipu_prg_channel chan[3]; 87ea9c2605SLucas Stach }; 88ea9c2605SLucas Stach 89ea9c2605SLucas Stach static DEFINE_MUTEX(ipu_prg_list_mutex); 90ea9c2605SLucas Stach static LIST_HEAD(ipu_prg_list); 91ea9c2605SLucas Stach 92ea9c2605SLucas Stach struct ipu_prg * 93ea9c2605SLucas Stach ipu_prg_lookup_by_phandle(struct device *dev, const char *name, int ipu_id) 94ea9c2605SLucas Stach { 95ea9c2605SLucas Stach struct device_node *prg_node = of_parse_phandle(dev->of_node, 96ea9c2605SLucas Stach name, 0); 97ea9c2605SLucas Stach struct ipu_prg *prg; 98ea9c2605SLucas Stach 99ea9c2605SLucas Stach mutex_lock(&ipu_prg_list_mutex); 100ea9c2605SLucas Stach list_for_each_entry(prg, &ipu_prg_list, list) { 101ea9c2605SLucas Stach if (prg_node == prg->dev->of_node) { 102ea9c2605SLucas Stach mutex_unlock(&ipu_prg_list_mutex); 103*e88728f4SVivek Gautam device_link_add(dev, prg->dev, 104*e88728f4SVivek Gautam DL_FLAG_AUTOREMOVE_CONSUMER); 105ea9c2605SLucas Stach prg->id = ipu_id; 1063addaba8STobias Jordan of_node_put(prg_node); 107ea9c2605SLucas Stach return prg; 108ea9c2605SLucas Stach } 109ea9c2605SLucas Stach } 110ea9c2605SLucas Stach mutex_unlock(&ipu_prg_list_mutex); 111ea9c2605SLucas Stach 1123addaba8STobias Jordan of_node_put(prg_node); 1133addaba8STobias Jordan 114ea9c2605SLucas Stach return NULL; 115ea9c2605SLucas Stach } 116ea9c2605SLucas Stach 117ea9c2605SLucas Stach int ipu_prg_max_active_channels(void) 118ea9c2605SLucas Stach { 119ea9c2605SLucas Stach return ipu_pre_get_available_count(); 120ea9c2605SLucas Stach } 121ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_max_active_channels); 122ea9c2605SLucas Stach 123ea9c2605SLucas Stach bool ipu_prg_present(struct ipu_soc *ipu) 124ea9c2605SLucas Stach { 125ea9c2605SLucas Stach if (ipu->prg_priv) 126ea9c2605SLucas Stach return true; 127ea9c2605SLucas Stach 128ea9c2605SLucas Stach return false; 129ea9c2605SLucas Stach } 130ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_present); 131ea9c2605SLucas Stach 132ea9c2605SLucas Stach bool ipu_prg_format_supported(struct ipu_soc *ipu, uint32_t format, 133ea9c2605SLucas Stach uint64_t modifier) 134ea9c2605SLucas Stach { 135ea9c2605SLucas Stach const struct drm_format_info *info = drm_format_info(format); 136ea9c2605SLucas Stach 137ea9c2605SLucas Stach if (info->num_planes != 1) 138ea9c2605SLucas Stach return false; 139ea9c2605SLucas Stach 140a2ceec52SLucas Stach switch (modifier) { 141a2ceec52SLucas Stach case DRM_FORMAT_MOD_LINEAR: 142a2ceec52SLucas Stach case DRM_FORMAT_MOD_VIVANTE_TILED: 143a2ceec52SLucas Stach case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED: 144ea9c2605SLucas Stach return true; 145a2ceec52SLucas Stach default: 146a2ceec52SLucas Stach return false; 147a2ceec52SLucas Stach } 148ea9c2605SLucas Stach } 149ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_format_supported); 150ea9c2605SLucas Stach 151ea9c2605SLucas Stach int ipu_prg_enable(struct ipu_soc *ipu) 152ea9c2605SLucas Stach { 153ea9c2605SLucas Stach struct ipu_prg *prg = ipu->prg_priv; 154ea9c2605SLucas Stach 155ea9c2605SLucas Stach if (!prg) 156ea9c2605SLucas Stach return 0; 157ea9c2605SLucas Stach 15872944089SLucas Stach return pm_runtime_get_sync(prg->dev); 159ea9c2605SLucas Stach } 160ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_enable); 161ea9c2605SLucas Stach 162ea9c2605SLucas Stach void ipu_prg_disable(struct ipu_soc *ipu) 163ea9c2605SLucas Stach { 164ea9c2605SLucas Stach struct ipu_prg *prg = ipu->prg_priv; 165ea9c2605SLucas Stach 166ea9c2605SLucas Stach if (!prg) 167ea9c2605SLucas Stach return; 168ea9c2605SLucas Stach 16972944089SLucas Stach pm_runtime_put(prg->dev); 170ea9c2605SLucas Stach } 171ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_disable); 172ea9c2605SLucas Stach 173ea9c2605SLucas Stach /* 174ea9c2605SLucas Stach * The channel configuartion functions below are not thread safe, as they 175ea9c2605SLucas Stach * must be only called from the atomic commit path in the DRM driver, which 176ea9c2605SLucas Stach * is properly serialized. 177ea9c2605SLucas Stach */ 178ea9c2605SLucas Stach static int ipu_prg_ipu_to_prg_chan(int ipu_chan) 179ea9c2605SLucas Stach { 180ea9c2605SLucas Stach /* 181ea9c2605SLucas Stach * This isn't clearly documented in the RM, but IPU to PRG channel 182ea9c2605SLucas Stach * assignment is fixed, as only with this mapping the control signals 183ea9c2605SLucas Stach * match up. 184ea9c2605SLucas Stach */ 185ea9c2605SLucas Stach switch (ipu_chan) { 186ea9c2605SLucas Stach case IPUV3_CHANNEL_MEM_BG_SYNC: 187ea9c2605SLucas Stach return 0; 188ea9c2605SLucas Stach case IPUV3_CHANNEL_MEM_FG_SYNC: 189ea9c2605SLucas Stach return 1; 190ea9c2605SLucas Stach case IPUV3_CHANNEL_MEM_DC_SYNC: 191ea9c2605SLucas Stach return 2; 192ea9c2605SLucas Stach default: 193ea9c2605SLucas Stach return -EINVAL; 194ea9c2605SLucas Stach } 195ea9c2605SLucas Stach } 196ea9c2605SLucas Stach 197ea9c2605SLucas Stach static int ipu_prg_get_pre(struct ipu_prg *prg, int prg_chan) 198ea9c2605SLucas Stach { 199ea9c2605SLucas Stach int i, ret; 200ea9c2605SLucas Stach 201ea9c2605SLucas Stach /* channel 0 is special as it is hardwired to one of the PREs */ 202ea9c2605SLucas Stach if (prg_chan == 0) { 203ea9c2605SLucas Stach ret = ipu_pre_get(prg->pres[0]); 204ea9c2605SLucas Stach if (ret) 205ea9c2605SLucas Stach goto fail; 206ea9c2605SLucas Stach prg->chan[prg_chan].used_pre = 0; 207ea9c2605SLucas Stach return 0; 208ea9c2605SLucas Stach } 209ea9c2605SLucas Stach 210ea9c2605SLucas Stach for (i = 1; i < 3; i++) { 211ea9c2605SLucas Stach ret = ipu_pre_get(prg->pres[i]); 212ea9c2605SLucas Stach if (!ret) { 213ea9c2605SLucas Stach u32 val, mux; 214ea9c2605SLucas Stach int shift; 215ea9c2605SLucas Stach 216ea9c2605SLucas Stach prg->chan[prg_chan].used_pre = i; 217ea9c2605SLucas Stach 218ea9c2605SLucas Stach /* configure the PRE to PRG channel mux */ 219ea9c2605SLucas Stach shift = (i == 1) ? 12 : 14; 220ea9c2605SLucas Stach mux = (prg->id << 1) | (prg_chan - 1); 221ea9c2605SLucas Stach regmap_update_bits(prg->iomuxc_gpr, IOMUXC_GPR5, 222ea9c2605SLucas Stach 0x3 << shift, mux << shift); 223ea9c2605SLucas Stach 224ea9c2605SLucas Stach /* check other mux, must not point to same channel */ 225ea9c2605SLucas Stach shift = (i == 1) ? 14 : 12; 226ea9c2605SLucas Stach regmap_read(prg->iomuxc_gpr, IOMUXC_GPR5, &val); 227ea9c2605SLucas Stach if (((val >> shift) & 0x3) == mux) { 228ea9c2605SLucas Stach regmap_update_bits(prg->iomuxc_gpr, IOMUXC_GPR5, 229ea9c2605SLucas Stach 0x3 << shift, 230ea9c2605SLucas Stach (mux ^ 0x1) << shift); 231ea9c2605SLucas Stach } 232ea9c2605SLucas Stach 233ea9c2605SLucas Stach return 0; 234ea9c2605SLucas Stach } 235ea9c2605SLucas Stach } 236ea9c2605SLucas Stach 237ea9c2605SLucas Stach fail: 238ea9c2605SLucas Stach dev_err(prg->dev, "could not get PRE for PRG chan %d", prg_chan); 239ea9c2605SLucas Stach return ret; 240ea9c2605SLucas Stach } 241ea9c2605SLucas Stach 242ea9c2605SLucas Stach static void ipu_prg_put_pre(struct ipu_prg *prg, int prg_chan) 243ea9c2605SLucas Stach { 244ea9c2605SLucas Stach struct ipu_prg_channel *chan = &prg->chan[prg_chan]; 245ea9c2605SLucas Stach 246ea9c2605SLucas Stach ipu_pre_put(prg->pres[chan->used_pre]); 247ea9c2605SLucas Stach chan->used_pre = -1; 248ea9c2605SLucas Stach } 249ea9c2605SLucas Stach 250ea9c2605SLucas Stach void ipu_prg_channel_disable(struct ipuv3_channel *ipu_chan) 251ea9c2605SLucas Stach { 252ea9c2605SLucas Stach int prg_chan = ipu_prg_ipu_to_prg_chan(ipu_chan->num); 253ea9c2605SLucas Stach struct ipu_prg *prg = ipu_chan->ipu->prg_priv; 254746d024cSArnd Bergmann struct ipu_prg_channel *chan; 255ea9c2605SLucas Stach u32 val; 256ea9c2605SLucas Stach 257746d024cSArnd Bergmann if (prg_chan < 0) 258746d024cSArnd Bergmann return; 259746d024cSArnd Bergmann 260746d024cSArnd Bergmann chan = &prg->chan[prg_chan]; 261746d024cSArnd Bergmann if (!chan->enabled) 262ea9c2605SLucas Stach return; 263ea9c2605SLucas Stach 26472944089SLucas Stach pm_runtime_get_sync(prg->dev); 265ea9c2605SLucas Stach 266ea9c2605SLucas Stach val = readl(prg->regs + IPU_PRG_CTL); 267ea9c2605SLucas Stach val |= IPU_PRG_CTL_BYPASS(prg_chan); 268ea9c2605SLucas Stach writel(val, prg->regs + IPU_PRG_CTL); 269ea9c2605SLucas Stach 270ea9c2605SLucas Stach val = IPU_PRG_REG_UPDATE_REG_UPDATE; 271ea9c2605SLucas Stach writel(val, prg->regs + IPU_PRG_REG_UPDATE); 272ea9c2605SLucas Stach 27372944089SLucas Stach pm_runtime_put(prg->dev); 274ea9c2605SLucas Stach 275ea9c2605SLucas Stach ipu_prg_put_pre(prg, prg_chan); 276ea9c2605SLucas Stach 277ea9c2605SLucas Stach chan->enabled = false; 278ea9c2605SLucas Stach } 279ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_channel_disable); 280ea9c2605SLucas Stach 281ea9c2605SLucas Stach int ipu_prg_channel_configure(struct ipuv3_channel *ipu_chan, 282ea9c2605SLucas Stach unsigned int axi_id, unsigned int width, 283ea9c2605SLucas Stach unsigned int height, unsigned int stride, 284a2ceec52SLucas Stach u32 format, uint64_t modifier, unsigned long *eba) 285ea9c2605SLucas Stach { 286ea9c2605SLucas Stach int prg_chan = ipu_prg_ipu_to_prg_chan(ipu_chan->num); 287ea9c2605SLucas Stach struct ipu_prg *prg = ipu_chan->ipu->prg_priv; 288746d024cSArnd Bergmann struct ipu_prg_channel *chan; 289ea9c2605SLucas Stach u32 val; 290ea9c2605SLucas Stach int ret; 291ea9c2605SLucas Stach 292ea9c2605SLucas Stach if (prg_chan < 0) 293ea9c2605SLucas Stach return prg_chan; 294ea9c2605SLucas Stach 295746d024cSArnd Bergmann chan = &prg->chan[prg_chan]; 296746d024cSArnd Bergmann 297ea9c2605SLucas Stach if (chan->enabled) { 298ea9c2605SLucas Stach ipu_pre_update(prg->pres[chan->used_pre], *eba); 299ea9c2605SLucas Stach return 0; 300ea9c2605SLucas Stach } 301ea9c2605SLucas Stach 302ea9c2605SLucas Stach ret = ipu_prg_get_pre(prg, prg_chan); 303ea9c2605SLucas Stach if (ret) 304ea9c2605SLucas Stach return ret; 305ea9c2605SLucas Stach 306ea9c2605SLucas Stach ipu_pre_configure(prg->pres[chan->used_pre], 307a2ceec52SLucas Stach width, height, stride, format, modifier, *eba); 308ea9c2605SLucas Stach 309ea9c2605SLucas Stach 31072944089SLucas Stach pm_runtime_get_sync(prg->dev); 311ea9c2605SLucas Stach 312ea9c2605SLucas Stach val = (stride - 1) & IPU_PRG_STRIDE_STRIDE_MASK; 313ea9c2605SLucas Stach writel(val, prg->regs + IPU_PRG_STRIDE(prg_chan)); 314ea9c2605SLucas Stach 315ea9c2605SLucas Stach val = ((height & IPU_PRG_HEIGHT_PRE_HEIGHT_MASK) << 316ea9c2605SLucas Stach IPU_PRG_HEIGHT_PRE_HEIGHT_SHIFT) | 317ea9c2605SLucas Stach ((height & IPU_PRG_HEIGHT_IPU_HEIGHT_MASK) << 318ea9c2605SLucas Stach IPU_PRG_HEIGHT_IPU_HEIGHT_SHIFT); 319ea9c2605SLucas Stach writel(val, prg->regs + IPU_PRG_HEIGHT(prg_chan)); 320ea9c2605SLucas Stach 321ea9c2605SLucas Stach val = ipu_pre_get_baddr(prg->pres[chan->used_pre]); 322ea9c2605SLucas Stach *eba = val; 323ea9c2605SLucas Stach writel(val, prg->regs + IPU_PRG_BADDR(prg_chan)); 324ea9c2605SLucas Stach 325ea9c2605SLucas Stach val = readl(prg->regs + IPU_PRG_CTL); 326ea9c2605SLucas Stach /* config AXI ID */ 327ea9c2605SLucas Stach val &= ~(IPU_PRG_CTL_SOFT_ARID_MASK << 328ea9c2605SLucas Stach IPU_PRG_CTL_SOFT_ARID_SHIFT(prg_chan)); 329ea9c2605SLucas Stach val |= IPU_PRG_CTL_SOFT_ARID(prg_chan, axi_id); 330ea9c2605SLucas Stach /* enable channel */ 331ea9c2605SLucas Stach val &= ~IPU_PRG_CTL_BYPASS(prg_chan); 332ea9c2605SLucas Stach writel(val, prg->regs + IPU_PRG_CTL); 333ea9c2605SLucas Stach 334ea9c2605SLucas Stach val = IPU_PRG_REG_UPDATE_REG_UPDATE; 335ea9c2605SLucas Stach writel(val, prg->regs + IPU_PRG_REG_UPDATE); 336ea9c2605SLucas Stach 337263c3b80SLucas Stach /* wait for both double buffers to be filled */ 338263c3b80SLucas Stach readl_poll_timeout(prg->regs + IPU_PRG_STATUS, val, 339263c3b80SLucas Stach (val & IPU_PRG_STATUS_BUFFER0_READY(prg_chan)) && 340263c3b80SLucas Stach (val & IPU_PRG_STATUS_BUFFER1_READY(prg_chan)), 341263c3b80SLucas Stach 5, 1000); 342263c3b80SLucas Stach 34372944089SLucas Stach pm_runtime_put(prg->dev); 344ea9c2605SLucas Stach 345ea9c2605SLucas Stach chan->enabled = true; 346ea9c2605SLucas Stach return 0; 347ea9c2605SLucas Stach } 348ea9c2605SLucas Stach EXPORT_SYMBOL_GPL(ipu_prg_channel_configure); 349ea9c2605SLucas Stach 350ea9c2605SLucas Stach static int ipu_prg_probe(struct platform_device *pdev) 351ea9c2605SLucas Stach { 352ea9c2605SLucas Stach struct device *dev = &pdev->dev; 353ea9c2605SLucas Stach struct resource *res; 354ea9c2605SLucas Stach struct ipu_prg *prg; 355ea9c2605SLucas Stach u32 val; 356ea9c2605SLucas Stach int i, ret; 357ea9c2605SLucas Stach 358ea9c2605SLucas Stach prg = devm_kzalloc(dev, sizeof(*prg), GFP_KERNEL); 359ea9c2605SLucas Stach if (!prg) 360ea9c2605SLucas Stach return -ENOMEM; 361ea9c2605SLucas Stach 362ea9c2605SLucas Stach res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 363ea9c2605SLucas Stach prg->regs = devm_ioremap_resource(&pdev->dev, res); 364ea9c2605SLucas Stach if (IS_ERR(prg->regs)) 365ea9c2605SLucas Stach return PTR_ERR(prg->regs); 366ea9c2605SLucas Stach 367ea9c2605SLucas Stach 368ea9c2605SLucas Stach prg->clk_ipg = devm_clk_get(dev, "ipg"); 369ea9c2605SLucas Stach if (IS_ERR(prg->clk_ipg)) 370ea9c2605SLucas Stach return PTR_ERR(prg->clk_ipg); 371ea9c2605SLucas Stach 372ea9c2605SLucas Stach prg->clk_axi = devm_clk_get(dev, "axi"); 373ea9c2605SLucas Stach if (IS_ERR(prg->clk_axi)) 374ea9c2605SLucas Stach return PTR_ERR(prg->clk_axi); 375ea9c2605SLucas Stach 376ea9c2605SLucas Stach prg->iomuxc_gpr = 377ea9c2605SLucas Stach syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr"); 378ea9c2605SLucas Stach if (IS_ERR(prg->iomuxc_gpr)) 379ea9c2605SLucas Stach return PTR_ERR(prg->iomuxc_gpr); 380ea9c2605SLucas Stach 381ea9c2605SLucas Stach for (i = 0; i < 3; i++) { 382ea9c2605SLucas Stach prg->pres[i] = ipu_pre_lookup_by_phandle(dev, "fsl,pres", i); 383ea9c2605SLucas Stach if (!prg->pres[i]) 384ea9c2605SLucas Stach return -EPROBE_DEFER; 385ea9c2605SLucas Stach } 386ea9c2605SLucas Stach 387ea9c2605SLucas Stach ret = clk_prepare_enable(prg->clk_ipg); 388ea9c2605SLucas Stach if (ret) 389ea9c2605SLucas Stach return ret; 390ea9c2605SLucas Stach 39172944089SLucas Stach ret = clk_prepare_enable(prg->clk_axi); 39272944089SLucas Stach if (ret) { 39372944089SLucas Stach clk_disable_unprepare(prg->clk_ipg); 39472944089SLucas Stach return ret; 39572944089SLucas Stach } 39672944089SLucas Stach 397ea9c2605SLucas Stach /* init to free running mode */ 398ea9c2605SLucas Stach val = readl(prg->regs + IPU_PRG_CTL); 399ea9c2605SLucas Stach val |= IPU_PRG_CTL_SHADOW_EN; 400ea9c2605SLucas Stach writel(val, prg->regs + IPU_PRG_CTL); 401ea9c2605SLucas Stach 402ea9c2605SLucas Stach /* disable address threshold */ 403ea9c2605SLucas Stach writel(0xffffffff, prg->regs + IPU_PRG_THD); 404ea9c2605SLucas Stach 40572944089SLucas Stach pm_runtime_set_active(dev); 40672944089SLucas Stach pm_runtime_enable(dev); 407ea9c2605SLucas Stach 408ea9c2605SLucas Stach prg->dev = dev; 409ea9c2605SLucas Stach platform_set_drvdata(pdev, prg); 410ea9c2605SLucas Stach mutex_lock(&ipu_prg_list_mutex); 411ea9c2605SLucas Stach list_add(&prg->list, &ipu_prg_list); 412ea9c2605SLucas Stach mutex_unlock(&ipu_prg_list_mutex); 413ea9c2605SLucas Stach 414ea9c2605SLucas Stach return 0; 415ea9c2605SLucas Stach } 416ea9c2605SLucas Stach 417ea9c2605SLucas Stach static int ipu_prg_remove(struct platform_device *pdev) 418ea9c2605SLucas Stach { 419ea9c2605SLucas Stach struct ipu_prg *prg = platform_get_drvdata(pdev); 420ea9c2605SLucas Stach 421ea9c2605SLucas Stach mutex_lock(&ipu_prg_list_mutex); 422ea9c2605SLucas Stach list_del(&prg->list); 423ea9c2605SLucas Stach mutex_unlock(&ipu_prg_list_mutex); 424ea9c2605SLucas Stach 425ea9c2605SLucas Stach return 0; 426ea9c2605SLucas Stach } 427ea9c2605SLucas Stach 42872944089SLucas Stach #ifdef CONFIG_PM 42972944089SLucas Stach static int prg_suspend(struct device *dev) 43072944089SLucas Stach { 43172944089SLucas Stach struct ipu_prg *prg = dev_get_drvdata(dev); 43272944089SLucas Stach 43372944089SLucas Stach clk_disable_unprepare(prg->clk_axi); 43472944089SLucas Stach clk_disable_unprepare(prg->clk_ipg); 43572944089SLucas Stach 43672944089SLucas Stach return 0; 43772944089SLucas Stach } 43872944089SLucas Stach 43972944089SLucas Stach static int prg_resume(struct device *dev) 44072944089SLucas Stach { 44172944089SLucas Stach struct ipu_prg *prg = dev_get_drvdata(dev); 44272944089SLucas Stach int ret; 44372944089SLucas Stach 44472944089SLucas Stach ret = clk_prepare_enable(prg->clk_ipg); 44572944089SLucas Stach if (ret) 44672944089SLucas Stach return ret; 44772944089SLucas Stach 44872944089SLucas Stach ret = clk_prepare_enable(prg->clk_axi); 44972944089SLucas Stach if (ret) { 45072944089SLucas Stach clk_disable_unprepare(prg->clk_ipg); 45172944089SLucas Stach return ret; 45272944089SLucas Stach } 45372944089SLucas Stach 45472944089SLucas Stach return 0; 45572944089SLucas Stach } 45672944089SLucas Stach #endif 45772944089SLucas Stach 45872944089SLucas Stach static const struct dev_pm_ops prg_pm_ops = { 45972944089SLucas Stach SET_RUNTIME_PM_OPS(prg_suspend, prg_resume, NULL) 46072944089SLucas Stach }; 46172944089SLucas Stach 462ea9c2605SLucas Stach static const struct of_device_id ipu_prg_dt_ids[] = { 463ea9c2605SLucas Stach { .compatible = "fsl,imx6qp-prg", }, 464ea9c2605SLucas Stach { /* sentinel */ }, 465ea9c2605SLucas Stach }; 466ea9c2605SLucas Stach 467ea9c2605SLucas Stach struct platform_driver ipu_prg_drv = { 468ea9c2605SLucas Stach .probe = ipu_prg_probe, 469ea9c2605SLucas Stach .remove = ipu_prg_remove, 470ea9c2605SLucas Stach .driver = { 471ea9c2605SLucas Stach .name = "imx-ipu-prg", 47272944089SLucas Stach .pm = &prg_pm_ops, 473ea9c2605SLucas Stach .of_match_table = ipu_prg_dt_ids, 474ea9c2605SLucas Stach }, 475ea9c2605SLucas Stach }; 476