Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * OMAP SmartReflex Voltage Control
0004  *
0005  * Author: Thara Gopinath   <thara@ti.com>
0006  *
0007  * Copyright (C) 2012 Texas Instruments, Inc.
0008  * Thara Gopinath <thara@ti.com>
0009  *
0010  * Copyright (C) 2008 Nokia Corporation
0011  * Kalle Jokiniemi
0012  *
0013  * Copyright (C) 2007 Texas Instruments, Inc.
0014  * Lesly A M <x0080970@ti.com>
0015  */
0016 
0017 #include <linux/module.h>
0018 #include <linux/mod_devicetable.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/clk.h>
0021 #include <linux/io.h>
0022 #include <linux/debugfs.h>
0023 #include <linux/delay.h>
0024 #include <linux/slab.h>
0025 #include <linux/pm_runtime.h>
0026 #include <linux/power/smartreflex.h>
0027 
0028 #define DRIVER_NAME "smartreflex"
0029 #define SMARTREFLEX_NAME_LEN    32
0030 #define NVALUE_NAME_LEN     40
0031 #define SR_DISABLE_TIMEOUT  200
0032 
0033 /* sr_list contains all the instances of smartreflex module */
0034 static LIST_HEAD(sr_list);
0035 
0036 static struct omap_sr_class_data *sr_class;
0037 static struct dentry        *sr_dbg_dir;
0038 
0039 static inline void sr_write_reg(struct omap_sr *sr, unsigned offset, u32 value)
0040 {
0041     __raw_writel(value, (sr->base + offset));
0042 }
0043 
0044 static inline void sr_modify_reg(struct omap_sr *sr, unsigned offset, u32 mask,
0045                     u32 value)
0046 {
0047     u32 reg_val;
0048 
0049     /*
0050      * Smartreflex error config register is special as it contains
0051      * certain status bits which if written a 1 into means a clear
0052      * of those bits. So in order to make sure no accidental write of
0053      * 1 happens to those status bits, do a clear of them in the read
0054      * value. This mean this API doesn't rewrite values in these bits
0055      * if they are currently set, but does allow the caller to write
0056      * those bits.
0057      */
0058     if (sr->ip_type == SR_TYPE_V1 && offset == ERRCONFIG_V1)
0059         mask |= ERRCONFIG_STATUS_V1_MASK;
0060     else if (sr->ip_type == SR_TYPE_V2 && offset == ERRCONFIG_V2)
0061         mask |= ERRCONFIG_VPBOUNDINTST_V2;
0062 
0063     reg_val = __raw_readl(sr->base + offset);
0064     reg_val &= ~mask;
0065 
0066     value &= mask;
0067 
0068     reg_val |= value;
0069 
0070     __raw_writel(reg_val, (sr->base + offset));
0071 }
0072 
0073 static inline u32 sr_read_reg(struct omap_sr *sr, unsigned offset)
0074 {
0075     return __raw_readl(sr->base + offset);
0076 }
0077 
0078 static struct omap_sr *_sr_lookup(struct voltagedomain *voltdm)
0079 {
0080     struct omap_sr *sr_info;
0081 
0082     if (!voltdm) {
0083         pr_err("%s: Null voltage domain passed!\n", __func__);
0084         return ERR_PTR(-EINVAL);
0085     }
0086 
0087     list_for_each_entry(sr_info, &sr_list, node) {
0088         if (voltdm == sr_info->voltdm)
0089             return sr_info;
0090     }
0091 
0092     return ERR_PTR(-ENODATA);
0093 }
0094 
0095 static irqreturn_t sr_interrupt(int irq, void *data)
0096 {
0097     struct omap_sr *sr_info = data;
0098     u32 status = 0;
0099 
0100     switch (sr_info->ip_type) {
0101     case SR_TYPE_V1:
0102         /* Read the status bits */
0103         status = sr_read_reg(sr_info, ERRCONFIG_V1);
0104 
0105         /* Clear them by writing back */
0106         sr_write_reg(sr_info, ERRCONFIG_V1, status);
0107         break;
0108     case SR_TYPE_V2:
0109         /* Read the status bits */
0110         status = sr_read_reg(sr_info, IRQSTATUS);
0111 
0112         /* Clear them by writing back */
0113         sr_write_reg(sr_info, IRQSTATUS, status);
0114         break;
0115     default:
0116         dev_err(&sr_info->pdev->dev, "UNKNOWN IP type %d\n",
0117             sr_info->ip_type);
0118         return IRQ_NONE;
0119     }
0120 
0121     if (sr_class->notify)
0122         sr_class->notify(sr_info, status);
0123 
0124     return IRQ_HANDLED;
0125 }
0126 
0127 static void sr_set_clk_length(struct omap_sr *sr)
0128 {
0129     u32 fclk_speed;
0130 
0131     /* Try interconnect target module fck first if it already exists */
0132     if (IS_ERR(sr->fck))
0133         return;
0134 
0135     fclk_speed = clk_get_rate(sr->fck);
0136 
0137     switch (fclk_speed) {
0138     case 12000000:
0139         sr->clk_length = SRCLKLENGTH_12MHZ_SYSCLK;
0140         break;
0141     case 13000000:
0142         sr->clk_length = SRCLKLENGTH_13MHZ_SYSCLK;
0143         break;
0144     case 19200000:
0145         sr->clk_length = SRCLKLENGTH_19MHZ_SYSCLK;
0146         break;
0147     case 26000000:
0148         sr->clk_length = SRCLKLENGTH_26MHZ_SYSCLK;
0149         break;
0150     case 38400000:
0151         sr->clk_length = SRCLKLENGTH_38MHZ_SYSCLK;
0152         break;
0153     default:
0154         dev_err(&sr->pdev->dev, "%s: Invalid fclk rate: %d\n",
0155             __func__, fclk_speed);
0156         break;
0157     }
0158 }
0159 
0160 static void sr_start_vddautocomp(struct omap_sr *sr)
0161 {
0162     if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
0163         dev_warn(&sr->pdev->dev,
0164              "%s: smartreflex class driver not registered\n",
0165              __func__);
0166         return;
0167     }
0168 
0169     if (!sr_class->enable(sr))
0170         sr->autocomp_active = true;
0171 }
0172 
0173 static void sr_stop_vddautocomp(struct omap_sr *sr)
0174 {
0175     if (!sr_class || !(sr_class->disable)) {
0176         dev_warn(&sr->pdev->dev,
0177              "%s: smartreflex class driver not registered\n",
0178              __func__);
0179         return;
0180     }
0181 
0182     if (sr->autocomp_active) {
0183         sr_class->disable(sr, 1);
0184         sr->autocomp_active = false;
0185     }
0186 }
0187 
0188 /*
0189  * This function handles the initializations which have to be done
0190  * only when both sr device and class driver regiter has
0191  * completed. This will be attempted to be called from both sr class
0192  * driver register and sr device intializtion API's. Only one call
0193  * will ultimately succeed.
0194  *
0195  * Currently this function registers interrupt handler for a particular SR
0196  * if smartreflex class driver is already registered and has
0197  * requested for interrupts and the SR interrupt line in present.
0198  */
0199 static int sr_late_init(struct omap_sr *sr_info)
0200 {
0201     struct omap_sr_data *pdata = sr_info->pdev->dev.platform_data;
0202     int ret = 0;
0203 
0204     if (sr_class->notify && sr_class->notify_flags && sr_info->irq) {
0205         ret = devm_request_irq(&sr_info->pdev->dev, sr_info->irq,
0206                        sr_interrupt, 0, sr_info->name, sr_info);
0207         if (ret)
0208             goto error;
0209         disable_irq(sr_info->irq);
0210     }
0211 
0212     if (pdata && pdata->enable_on_init)
0213         sr_start_vddautocomp(sr_info);
0214 
0215     return ret;
0216 
0217 error:
0218     list_del(&sr_info->node);
0219     dev_err(&sr_info->pdev->dev, "%s: ERROR in registering interrupt handler. Smartreflex will not function as desired\n",
0220         __func__);
0221 
0222     return ret;
0223 }
0224 
0225 static void sr_v1_disable(struct omap_sr *sr)
0226 {
0227     int timeout = 0;
0228     int errconf_val = ERRCONFIG_MCUACCUMINTST | ERRCONFIG_MCUVALIDINTST |
0229             ERRCONFIG_MCUBOUNDINTST;
0230 
0231     /* Enable MCUDisableAcknowledge interrupt */
0232     sr_modify_reg(sr, ERRCONFIG_V1,
0233             ERRCONFIG_MCUDISACKINTEN, ERRCONFIG_MCUDISACKINTEN);
0234 
0235     /* SRCONFIG - disable SR */
0236     sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
0237 
0238     /* Disable all other SR interrupts and clear the status as needed */
0239     if (sr_read_reg(sr, ERRCONFIG_V1) & ERRCONFIG_VPBOUNDINTST_V1)
0240         errconf_val |= ERRCONFIG_VPBOUNDINTST_V1;
0241     sr_modify_reg(sr, ERRCONFIG_V1,
0242             (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
0243             ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_VPBOUNDINTEN_V1),
0244             errconf_val);
0245 
0246     /*
0247      * Wait for SR to be disabled.
0248      * wait until ERRCONFIG.MCUDISACKINTST = 1. Typical latency is 1us.
0249      */
0250     sr_test_cond_timeout((sr_read_reg(sr, ERRCONFIG_V1) &
0251                  ERRCONFIG_MCUDISACKINTST), SR_DISABLE_TIMEOUT,
0252                  timeout);
0253 
0254     if (timeout >= SR_DISABLE_TIMEOUT)
0255         dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
0256              __func__);
0257 
0258     /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
0259     sr_modify_reg(sr, ERRCONFIG_V1, ERRCONFIG_MCUDISACKINTEN,
0260             ERRCONFIG_MCUDISACKINTST);
0261 }
0262 
0263 static void sr_v2_disable(struct omap_sr *sr)
0264 {
0265     int timeout = 0;
0266 
0267     /* Enable MCUDisableAcknowledge interrupt */
0268     sr_write_reg(sr, IRQENABLE_SET, IRQENABLE_MCUDISABLEACKINT);
0269 
0270     /* SRCONFIG - disable SR */
0271     sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
0272 
0273     /*
0274      * Disable all other SR interrupts and clear the status
0275      * write to status register ONLY on need basis - only if status
0276      * is set.
0277      */
0278     if (sr_read_reg(sr, ERRCONFIG_V2) & ERRCONFIG_VPBOUNDINTST_V2)
0279         sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
0280             ERRCONFIG_VPBOUNDINTST_V2);
0281     else
0282         sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
0283                 0x0);
0284     sr_write_reg(sr, IRQENABLE_CLR, (IRQENABLE_MCUACCUMINT |
0285             IRQENABLE_MCUVALIDINT |
0286             IRQENABLE_MCUBOUNDSINT));
0287     sr_write_reg(sr, IRQSTATUS, (IRQSTATUS_MCUACCUMINT |
0288             IRQSTATUS_MCVALIDINT |
0289             IRQSTATUS_MCBOUNDSINT));
0290 
0291     /*
0292      * Wait for SR to be disabled.
0293      * wait until IRQSTATUS.MCUDISACKINTST = 1. Typical latency is 1us.
0294      */
0295     sr_test_cond_timeout((sr_read_reg(sr, IRQSTATUS) &
0296                  IRQSTATUS_MCUDISABLEACKINT), SR_DISABLE_TIMEOUT,
0297                  timeout);
0298 
0299     if (timeout >= SR_DISABLE_TIMEOUT)
0300         dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
0301              __func__);
0302 
0303     /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
0304     sr_write_reg(sr, IRQENABLE_CLR, IRQENABLE_MCUDISABLEACKINT);
0305     sr_write_reg(sr, IRQSTATUS, IRQSTATUS_MCUDISABLEACKINT);
0306 }
0307 
0308 static struct omap_sr_nvalue_table *sr_retrieve_nvalue_row(
0309                 struct omap_sr *sr, u32 efuse_offs)
0310 {
0311     int i;
0312 
0313     if (!sr->nvalue_table) {
0314         dev_warn(&sr->pdev->dev, "%s: Missing ntarget value table\n",
0315              __func__);
0316         return NULL;
0317     }
0318 
0319     for (i = 0; i < sr->nvalue_count; i++) {
0320         if (sr->nvalue_table[i].efuse_offs == efuse_offs)
0321             return &sr->nvalue_table[i];
0322     }
0323 
0324     return NULL;
0325 }
0326 
0327 /* Public Functions */
0328 
0329 /**
0330  * sr_configure_errgen() - Configures the SmartReflex to perform AVS using the
0331  *           error generator module.
0332  * @sr:         SR module to be configured.
0333  *
0334  * This API is to be called from the smartreflex class driver to
0335  * configure the error generator module inside the smartreflex module.
0336  * SR settings if using the ERROR module inside Smartreflex.
0337  * SR CLASS 3 by default uses only the ERROR module where as
0338  * SR CLASS 2 can choose between ERROR module and MINMAXAVG
0339  * module. Returns 0 on success and error value in case of failure.
0340  */
0341 int sr_configure_errgen(struct omap_sr *sr)
0342 {
0343     u32 sr_config, sr_errconfig, errconfig_offs;
0344     u32 vpboundint_en, vpboundint_st;
0345     u32 senp_en = 0, senn_en = 0;
0346     u8 senp_shift, senn_shift;
0347 
0348     if (!sr) {
0349         pr_warn("%s: NULL omap_sr from %pS\n",
0350             __func__, (void *)_RET_IP_);
0351         return -EINVAL;
0352     }
0353 
0354     if (!sr->clk_length)
0355         sr_set_clk_length(sr);
0356 
0357     senp_en = sr->senp_mod;
0358     senn_en = sr->senn_mod;
0359 
0360     sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
0361         SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN;
0362 
0363     switch (sr->ip_type) {
0364     case SR_TYPE_V1:
0365         sr_config |= SRCONFIG_DELAYCTRL;
0366         senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
0367         senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
0368         errconfig_offs = ERRCONFIG_V1;
0369         vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
0370         vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
0371         break;
0372     case SR_TYPE_V2:
0373         senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
0374         senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
0375         errconfig_offs = ERRCONFIG_V2;
0376         vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
0377         vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
0378         break;
0379     default:
0380         dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
0381             __func__);
0382         return -EINVAL;
0383     }
0384 
0385     sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
0386     sr_write_reg(sr, SRCONFIG, sr_config);
0387     sr_errconfig = (sr->err_weight << ERRCONFIG_ERRWEIGHT_SHIFT) |
0388         (sr->err_maxlimit << ERRCONFIG_ERRMAXLIMIT_SHIFT) |
0389         (sr->err_minlimit <<  ERRCONFIG_ERRMINLIMIT_SHIFT);
0390     sr_modify_reg(sr, errconfig_offs, (SR_ERRWEIGHT_MASK |
0391         SR_ERRMAXLIMIT_MASK | SR_ERRMINLIMIT_MASK),
0392         sr_errconfig);
0393 
0394     /* Enabling the interrupts if the ERROR module is used */
0395     sr_modify_reg(sr, errconfig_offs, (vpboundint_en | vpboundint_st),
0396               vpboundint_en);
0397 
0398     return 0;
0399 }
0400 
0401 /**
0402  * sr_disable_errgen() - Disables SmartReflex AVS module's errgen component
0403  * @sr:         SR module to be configured.
0404  *
0405  * This API is to be called from the smartreflex class driver to
0406  * disable the error generator module inside the smartreflex module.
0407  *
0408  * Returns 0 on success and error value in case of failure.
0409  */
0410 int sr_disable_errgen(struct omap_sr *sr)
0411 {
0412     u32 errconfig_offs;
0413     u32 vpboundint_en, vpboundint_st;
0414 
0415     if (!sr) {
0416         pr_warn("%s: NULL omap_sr from %pS\n",
0417             __func__, (void *)_RET_IP_);
0418         return -EINVAL;
0419     }
0420 
0421     switch (sr->ip_type) {
0422     case SR_TYPE_V1:
0423         errconfig_offs = ERRCONFIG_V1;
0424         vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
0425         vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
0426         break;
0427     case SR_TYPE_V2:
0428         errconfig_offs = ERRCONFIG_V2;
0429         vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
0430         vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
0431         break;
0432     default:
0433         dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
0434             __func__);
0435         return -EINVAL;
0436     }
0437 
0438     /* Disable the Sensor and errorgen */
0439     sr_modify_reg(sr, SRCONFIG, SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN, 0);
0440 
0441     /*
0442      * Disable the interrupts of ERROR module
0443      * NOTE: modify is a read, modify,write - an implicit OCP barrier
0444      * which is required is present here - sequencing is critical
0445      * at this point (after errgen is disabled, vpboundint disable)
0446      */
0447     sr_modify_reg(sr, errconfig_offs, vpboundint_en | vpboundint_st, 0);
0448 
0449     return 0;
0450 }
0451 
0452 /**
0453  * sr_configure_minmax() - Configures the SmartReflex to perform AVS using the
0454  *           minmaxavg module.
0455  * @sr:         SR module to be configured.
0456  *
0457  * This API is to be called from the smartreflex class driver to
0458  * configure the minmaxavg module inside the smartreflex module.
0459  * SR settings if using the ERROR module inside Smartreflex.
0460  * SR CLASS 3 by default uses only the ERROR module where as
0461  * SR CLASS 2 can choose between ERROR module and MINMAXAVG
0462  * module. Returns 0 on success and error value in case of failure.
0463  */
0464 int sr_configure_minmax(struct omap_sr *sr)
0465 {
0466     u32 sr_config, sr_avgwt;
0467     u32 senp_en = 0, senn_en = 0;
0468     u8 senp_shift, senn_shift;
0469 
0470     if (!sr) {
0471         pr_warn("%s: NULL omap_sr from %pS\n",
0472             __func__, (void *)_RET_IP_);
0473         return -EINVAL;
0474     }
0475 
0476     if (!sr->clk_length)
0477         sr_set_clk_length(sr);
0478 
0479     senp_en = sr->senp_mod;
0480     senn_en = sr->senn_mod;
0481 
0482     sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
0483         SRCONFIG_SENENABLE |
0484         (sr->accum_data << SRCONFIG_ACCUMDATA_SHIFT);
0485 
0486     switch (sr->ip_type) {
0487     case SR_TYPE_V1:
0488         sr_config |= SRCONFIG_DELAYCTRL;
0489         senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
0490         senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
0491         break;
0492     case SR_TYPE_V2:
0493         senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
0494         senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
0495         break;
0496     default:
0497         dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
0498             __func__);
0499         return -EINVAL;
0500     }
0501 
0502     sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
0503     sr_write_reg(sr, SRCONFIG, sr_config);
0504     sr_avgwt = (sr->senp_avgweight << AVGWEIGHT_SENPAVGWEIGHT_SHIFT) |
0505         (sr->senn_avgweight << AVGWEIGHT_SENNAVGWEIGHT_SHIFT);
0506     sr_write_reg(sr, AVGWEIGHT, sr_avgwt);
0507 
0508     /*
0509      * Enabling the interrupts if MINMAXAVG module is used.
0510      * TODO: check if all the interrupts are mandatory
0511      */
0512     switch (sr->ip_type) {
0513     case SR_TYPE_V1:
0514         sr_modify_reg(sr, ERRCONFIG_V1,
0515             (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
0516             ERRCONFIG_MCUBOUNDINTEN),
0517             (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUACCUMINTST |
0518              ERRCONFIG_MCUVALIDINTEN | ERRCONFIG_MCUVALIDINTST |
0519              ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_MCUBOUNDINTST));
0520         break;
0521     case SR_TYPE_V2:
0522         sr_write_reg(sr, IRQSTATUS,
0523             IRQSTATUS_MCUACCUMINT | IRQSTATUS_MCVALIDINT |
0524             IRQSTATUS_MCBOUNDSINT | IRQSTATUS_MCUDISABLEACKINT);
0525         sr_write_reg(sr, IRQENABLE_SET,
0526             IRQENABLE_MCUACCUMINT | IRQENABLE_MCUVALIDINT |
0527             IRQENABLE_MCUBOUNDSINT | IRQENABLE_MCUDISABLEACKINT);
0528         break;
0529     default:
0530         dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
0531             __func__);
0532         return -EINVAL;
0533     }
0534 
0535     return 0;
0536 }
0537 
0538 /**
0539  * sr_enable() - Enables the smartreflex module.
0540  * @sr:     pointer to which the SR module to be configured belongs to.
0541  * @volt:   The voltage at which the Voltage domain associated with
0542  *      the smartreflex module is operating at.
0543  *      This is required only to program the correct Ntarget value.
0544  *
0545  * This API is to be called from the smartreflex class driver to
0546  * enable a smartreflex module. Returns 0 on success. Returns error
0547  * value if the voltage passed is wrong or if ntarget value is wrong.
0548  */
0549 int sr_enable(struct omap_sr *sr, unsigned long volt)
0550 {
0551     struct omap_volt_data *volt_data;
0552     struct omap_sr_nvalue_table *nvalue_row;
0553     int ret;
0554 
0555     if (!sr) {
0556         pr_warn("%s: NULL omap_sr from %pS\n",
0557             __func__, (void *)_RET_IP_);
0558         return -EINVAL;
0559     }
0560 
0561     volt_data = omap_voltage_get_voltdata(sr->voltdm, volt);
0562 
0563     if (IS_ERR(volt_data)) {
0564         dev_warn(&sr->pdev->dev, "%s: Unable to get voltage table for nominal voltage %ld\n",
0565              __func__, volt);
0566         return PTR_ERR(volt_data);
0567     }
0568 
0569     nvalue_row = sr_retrieve_nvalue_row(sr, volt_data->sr_efuse_offs);
0570 
0571     if (!nvalue_row) {
0572         dev_warn(&sr->pdev->dev, "%s: failure getting SR data for this voltage %ld\n",
0573              __func__, volt);
0574         return -ENODATA;
0575     }
0576 
0577     /* errminlimit is opp dependent and hence linked to voltage */
0578     sr->err_minlimit = nvalue_row->errminlimit;
0579 
0580     clk_enable(sr->fck);
0581 
0582     /* Check if SR is already enabled. If yes do nothing */
0583     if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE)
0584         goto out_enabled;
0585 
0586     /* Configure SR */
0587     ret = sr_class->configure(sr);
0588     if (ret)
0589         goto out_enabled;
0590 
0591     sr_write_reg(sr, NVALUERECIPROCAL, nvalue_row->nvalue);
0592 
0593     /* SRCONFIG - enable SR */
0594     sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, SRCONFIG_SRENABLE);
0595 
0596 out_enabled:
0597     sr->enabled = 1;
0598 
0599     return 0;
0600 }
0601 
0602 /**
0603  * sr_disable() - Disables the smartreflex module.
0604  * @sr:     pointer to which the SR module to be configured belongs to.
0605  *
0606  * This API is to be called from the smartreflex class driver to
0607  * disable a smartreflex module.
0608  */
0609 void sr_disable(struct omap_sr *sr)
0610 {
0611     if (!sr) {
0612         pr_warn("%s: NULL omap_sr from %pS\n",
0613             __func__, (void *)_RET_IP_);
0614         return;
0615     }
0616 
0617     /* Check if SR clocks are already disabled. If yes do nothing */
0618     if (!sr->enabled)
0619         return;
0620 
0621     /*
0622      * Disable SR if only it is indeed enabled. Else just
0623      * disable the clocks.
0624      */
0625     if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE) {
0626         switch (sr->ip_type) {
0627         case SR_TYPE_V1:
0628             sr_v1_disable(sr);
0629             break;
0630         case SR_TYPE_V2:
0631             sr_v2_disable(sr);
0632             break;
0633         default:
0634             dev_err(&sr->pdev->dev, "UNKNOWN IP type %d\n",
0635                 sr->ip_type);
0636         }
0637     }
0638 
0639     clk_disable(sr->fck);
0640     sr->enabled = 0;
0641 }
0642 
0643 /**
0644  * sr_register_class() - API to register a smartreflex class parameters.
0645  * @class_data: The structure containing various sr class specific data.
0646  *
0647  * This API is to be called by the smartreflex class driver to register itself
0648  * with the smartreflex driver during init. Returns 0 on success else the
0649  * error value.
0650  */
0651 int sr_register_class(struct omap_sr_class_data *class_data)
0652 {
0653     struct omap_sr *sr_info;
0654 
0655     if (!class_data) {
0656         pr_warn("%s:, Smartreflex class data passed is NULL\n",
0657             __func__);
0658         return -EINVAL;
0659     }
0660 
0661     if (sr_class) {
0662         pr_warn("%s: Smartreflex class driver already registered\n",
0663             __func__);
0664         return -EBUSY;
0665     }
0666 
0667     sr_class = class_data;
0668 
0669     /*
0670      * Call into late init to do initializations that require
0671      * both sr driver and sr class driver to be initiallized.
0672      */
0673     list_for_each_entry(sr_info, &sr_list, node)
0674         sr_late_init(sr_info);
0675 
0676     return 0;
0677 }
0678 
0679 /**
0680  * omap_sr_enable() -  API to enable SR clocks and to call into the
0681  *          registered smartreflex class enable API.
0682  * @voltdm: VDD pointer to which the SR module to be configured belongs to.
0683  *
0684  * This API is to be called from the kernel in order to enable
0685  * a particular smartreflex module. This API will do the initial
0686  * configurations to turn on the smartreflex module and in turn call
0687  * into the registered smartreflex class enable API.
0688  */
0689 void omap_sr_enable(struct voltagedomain *voltdm)
0690 {
0691     struct omap_sr *sr = _sr_lookup(voltdm);
0692 
0693     if (IS_ERR(sr)) {
0694         pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
0695         return;
0696     }
0697 
0698     if (!sr->autocomp_active)
0699         return;
0700 
0701     if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
0702         dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
0703              __func__);
0704         return;
0705     }
0706 
0707     sr_class->enable(sr);
0708 }
0709 
0710 /**
0711  * omap_sr_disable() - API to disable SR without resetting the voltage
0712  *          processor voltage
0713  * @voltdm: VDD pointer to which the SR module to be configured belongs to.
0714  *
0715  * This API is to be called from the kernel in order to disable
0716  * a particular smartreflex module. This API will in turn call
0717  * into the registered smartreflex class disable API. This API will tell
0718  * the smartreflex class disable not to reset the VP voltage after
0719  * disabling smartreflex.
0720  */
0721 void omap_sr_disable(struct voltagedomain *voltdm)
0722 {
0723     struct omap_sr *sr = _sr_lookup(voltdm);
0724 
0725     if (IS_ERR(sr)) {
0726         pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
0727         return;
0728     }
0729 
0730     if (!sr->autocomp_active)
0731         return;
0732 
0733     if (!sr_class || !(sr_class->disable)) {
0734         dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
0735              __func__);
0736         return;
0737     }
0738 
0739     sr_class->disable(sr, 0);
0740 }
0741 
0742 /**
0743  * omap_sr_disable_reset_volt() - API to disable SR and reset the
0744  *              voltage processor voltage
0745  * @voltdm: VDD pointer to which the SR module to be configured belongs to.
0746  *
0747  * This API is to be called from the kernel in order to disable
0748  * a particular smartreflex module. This API will in turn call
0749  * into the registered smartreflex class disable API. This API will tell
0750  * the smartreflex class disable to reset the VP voltage after
0751  * disabling smartreflex.
0752  */
0753 void omap_sr_disable_reset_volt(struct voltagedomain *voltdm)
0754 {
0755     struct omap_sr *sr = _sr_lookup(voltdm);
0756 
0757     if (IS_ERR(sr)) {
0758         pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
0759         return;
0760     }
0761 
0762     if (!sr->autocomp_active)
0763         return;
0764 
0765     if (!sr_class || !(sr_class->disable)) {
0766         dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
0767              __func__);
0768         return;
0769     }
0770 
0771     sr_class->disable(sr, 1);
0772 }
0773 
0774 /* PM Debug FS entries to enable and disable smartreflex. */
0775 static int omap_sr_autocomp_show(void *data, u64 *val)
0776 {
0777     struct omap_sr *sr_info = data;
0778 
0779     if (!sr_info) {
0780         pr_warn("%s: omap_sr struct not found\n", __func__);
0781         return -EINVAL;
0782     }
0783 
0784     *val = sr_info->autocomp_active;
0785 
0786     return 0;
0787 }
0788 
0789 static int omap_sr_autocomp_store(void *data, u64 val)
0790 {
0791     struct omap_sr *sr_info = data;
0792 
0793     if (!sr_info) {
0794         pr_warn("%s: omap_sr struct not found\n", __func__);
0795         return -EINVAL;
0796     }
0797 
0798     /* Sanity check */
0799     if (val > 1) {
0800         pr_warn("%s: Invalid argument %lld\n", __func__, val);
0801         return -EINVAL;
0802     }
0803 
0804     /* control enable/disable only if there is a delta in value */
0805     if (sr_info->autocomp_active != val) {
0806         if (!val)
0807             sr_stop_vddautocomp(sr_info);
0808         else
0809             sr_start_vddautocomp(sr_info);
0810     }
0811 
0812     return 0;
0813 }
0814 
0815 DEFINE_SIMPLE_ATTRIBUTE(pm_sr_fops, omap_sr_autocomp_show,
0816             omap_sr_autocomp_store, "%llu\n");
0817 
0818 static int omap_sr_probe(struct platform_device *pdev)
0819 {
0820     struct omap_sr *sr_info;
0821     struct omap_sr_data *pdata = pdev->dev.platform_data;
0822     struct resource *mem;
0823     struct dentry *nvalue_dir;
0824     int i, ret = 0;
0825 
0826     sr_info = devm_kzalloc(&pdev->dev, sizeof(struct omap_sr), GFP_KERNEL);
0827     if (!sr_info)
0828         return -ENOMEM;
0829 
0830     sr_info->name = devm_kzalloc(&pdev->dev,
0831                      SMARTREFLEX_NAME_LEN, GFP_KERNEL);
0832     if (!sr_info->name)
0833         return -ENOMEM;
0834 
0835     platform_set_drvdata(pdev, sr_info);
0836 
0837     if (!pdata) {
0838         dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
0839         return -EINVAL;
0840     }
0841 
0842     mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0843     sr_info->base = devm_ioremap_resource(&pdev->dev, mem);
0844     if (IS_ERR(sr_info->base))
0845         return PTR_ERR(sr_info->base);
0846 
0847     ret = platform_get_irq_optional(pdev, 0);
0848     if (ret < 0 && ret != -ENXIO)
0849         return dev_err_probe(&pdev->dev, ret, "failed to get IRQ resource\n");
0850     if (ret > 0)
0851         sr_info->irq = ret;
0852 
0853     sr_info->fck = devm_clk_get(pdev->dev.parent, "fck");
0854     if (IS_ERR(sr_info->fck))
0855         return PTR_ERR(sr_info->fck);
0856     clk_prepare(sr_info->fck);
0857 
0858     pm_runtime_enable(&pdev->dev);
0859 
0860     snprintf(sr_info->name, SMARTREFLEX_NAME_LEN, "%s", pdata->name);
0861 
0862     sr_info->pdev = pdev;
0863     sr_info->srid = pdev->id;
0864     sr_info->voltdm = pdata->voltdm;
0865     sr_info->nvalue_table = pdata->nvalue_table;
0866     sr_info->nvalue_count = pdata->nvalue_count;
0867     sr_info->senn_mod = pdata->senn_mod;
0868     sr_info->senp_mod = pdata->senp_mod;
0869     sr_info->err_weight = pdata->err_weight;
0870     sr_info->err_maxlimit = pdata->err_maxlimit;
0871     sr_info->accum_data = pdata->accum_data;
0872     sr_info->senn_avgweight = pdata->senn_avgweight;
0873     sr_info->senp_avgweight = pdata->senp_avgweight;
0874     sr_info->autocomp_active = false;
0875     sr_info->ip_type = pdata->ip_type;
0876 
0877     sr_set_clk_length(sr_info);
0878 
0879     list_add(&sr_info->node, &sr_list);
0880 
0881     /*
0882      * Call into late init to do initializations that require
0883      * both sr driver and sr class driver to be initiallized.
0884      */
0885     if (sr_class) {
0886         ret = sr_late_init(sr_info);
0887         if (ret) {
0888             pr_warn("%s: Error in SR late init\n", __func__);
0889             goto err_list_del;
0890         }
0891     }
0892 
0893     dev_info(&pdev->dev, "%s: SmartReflex driver initialized\n", __func__);
0894     if (!sr_dbg_dir)
0895         sr_dbg_dir = debugfs_create_dir("smartreflex", NULL);
0896 
0897     sr_info->dbg_dir = debugfs_create_dir(sr_info->name, sr_dbg_dir);
0898 
0899     debugfs_create_file("autocomp", S_IRUGO | S_IWUSR, sr_info->dbg_dir,
0900                 sr_info, &pm_sr_fops);
0901     debugfs_create_x32("errweight", S_IRUGO, sr_info->dbg_dir,
0902                &sr_info->err_weight);
0903     debugfs_create_x32("errmaxlimit", S_IRUGO, sr_info->dbg_dir,
0904                &sr_info->err_maxlimit);
0905 
0906     nvalue_dir = debugfs_create_dir("nvalue", sr_info->dbg_dir);
0907 
0908     if (sr_info->nvalue_count == 0 || !sr_info->nvalue_table) {
0909         dev_warn(&pdev->dev, "%s: %s: No Voltage table for the corresponding vdd. Cannot create debugfs entries for n-values\n",
0910              __func__, sr_info->name);
0911 
0912         ret = -ENODATA;
0913         goto err_debugfs;
0914     }
0915 
0916     for (i = 0; i < sr_info->nvalue_count; i++) {
0917         char name[NVALUE_NAME_LEN + 1];
0918 
0919         snprintf(name, sizeof(name), "volt_%lu",
0920                 sr_info->nvalue_table[i].volt_nominal);
0921         debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
0922                    &(sr_info->nvalue_table[i].nvalue));
0923         snprintf(name, sizeof(name), "errminlimit_%lu",
0924              sr_info->nvalue_table[i].volt_nominal);
0925         debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
0926                    &(sr_info->nvalue_table[i].errminlimit));
0927 
0928     }
0929 
0930     return 0;
0931 
0932 err_debugfs:
0933     debugfs_remove_recursive(sr_info->dbg_dir);
0934 err_list_del:
0935     list_del(&sr_info->node);
0936     clk_unprepare(sr_info->fck);
0937 
0938     return ret;
0939 }
0940 
0941 static int omap_sr_remove(struct platform_device *pdev)
0942 {
0943     struct omap_sr_data *pdata = pdev->dev.platform_data;
0944     struct device *dev = &pdev->dev;
0945     struct omap_sr *sr_info;
0946 
0947     if (!pdata) {
0948         dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
0949         return -EINVAL;
0950     }
0951 
0952     sr_info = _sr_lookup(pdata->voltdm);
0953     if (IS_ERR(sr_info)) {
0954         dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
0955             __func__);
0956         return PTR_ERR(sr_info);
0957     }
0958 
0959     if (sr_info->autocomp_active)
0960         sr_stop_vddautocomp(sr_info);
0961     debugfs_remove_recursive(sr_info->dbg_dir);
0962 
0963     pm_runtime_disable(dev);
0964     clk_unprepare(sr_info->fck);
0965     list_del(&sr_info->node);
0966     return 0;
0967 }
0968 
0969 static void omap_sr_shutdown(struct platform_device *pdev)
0970 {
0971     struct omap_sr_data *pdata = pdev->dev.platform_data;
0972     struct omap_sr *sr_info;
0973 
0974     if (!pdata) {
0975         dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
0976         return;
0977     }
0978 
0979     sr_info = _sr_lookup(pdata->voltdm);
0980     if (IS_ERR(sr_info)) {
0981         dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
0982             __func__);
0983         return;
0984     }
0985 
0986     if (sr_info->autocomp_active)
0987         sr_stop_vddautocomp(sr_info);
0988 
0989     return;
0990 }
0991 
0992 static const struct of_device_id omap_sr_match[] = {
0993     { .compatible = "ti,omap3-smartreflex-core", },
0994     { .compatible = "ti,omap3-smartreflex-mpu-iva", },
0995     { .compatible = "ti,omap4-smartreflex-core", },
0996     { .compatible = "ti,omap4-smartreflex-mpu", },
0997     { .compatible = "ti,omap4-smartreflex-iva", },
0998     {  },
0999 };
1000 MODULE_DEVICE_TABLE(of, omap_sr_match);
1001 
1002 static struct platform_driver smartreflex_driver = {
1003     .probe      = omap_sr_probe,
1004     .remove         = omap_sr_remove,
1005     .shutdown   = omap_sr_shutdown,
1006     .driver     = {
1007         .name   = DRIVER_NAME,
1008         .of_match_table = omap_sr_match,
1009     },
1010 };
1011 
1012 static int __init sr_init(void)
1013 {
1014     int ret = 0;
1015 
1016     ret = platform_driver_register(&smartreflex_driver);
1017     if (ret) {
1018         pr_err("%s: platform driver register failed for SR\n",
1019                __func__);
1020         return ret;
1021     }
1022 
1023     return 0;
1024 }
1025 late_initcall(sr_init);
1026 
1027 static void __exit sr_exit(void)
1028 {
1029     platform_driver_unregister(&smartreflex_driver);
1030 }
1031 module_exit(sr_exit);
1032 
1033 MODULE_DESCRIPTION("OMAP Smartreflex Driver");
1034 MODULE_LICENSE("GPL");
1035 MODULE_ALIAS("platform:" DRIVER_NAME);
1036 MODULE_AUTHOR("Texas Instruments Inc");