20 #include <linux/module.h>
26 #include <linux/slab.h>
32 #define SMARTREFLEX_NAME_LEN 16
33 #define NVALUE_NAME_LEN 40
34 #define SR_DISABLE_TIMEOUT 200
39 static struct omap_sr_class_data *sr_class;
40 static struct omap_sr_pmic_data *sr_pmic_data;
41 static struct dentry *sr_dbg_dir;
48 static inline void sr_modify_reg(
struct omap_sr *sr,
unsigned offset,
u32 mask,
77 static inline u32 sr_read_reg(
struct omap_sr *sr,
unsigned offset)
87 pr_err(
"%s: Null voltage domain passed!\n", __func__);
92 if (voltdm == sr_info->
voltdm)
114 status = sr_read_reg(sr_info,
IRQSTATUS);
117 sr_write_reg(sr_info,
IRQSTATUS, status);
120 dev_err(&sr_info->
pdev->dev,
"UNKNOWN IP type %d\n",
125 if (sr_class->notify)
126 sr_class->notify(sr_info, status);
131 static void sr_set_clk_length(
struct omap_sr *sr)
141 if (IS_ERR(sys_ck)) {
142 dev_err(&sr->
pdev->dev,
"%s: unable to get sys clk\n",
150 switch (sys_clk_speed) {
167 dev_err(&sr->
pdev->dev,
"%s: Invalid sysclk value: %d\n",
168 __func__, sys_clk_speed);
173 static void sr_set_regfields(
struct omap_sr *sr)
185 if (!(
strcmp(sr->
name,
"smartreflex_mpu_iva"))) {
195 static void sr_start_vddautocomp(
struct omap_sr *sr)
197 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
199 "%s: smartreflex class driver not registered\n",
204 if (!sr_class->enable(sr))
208 static void sr_stop_vddautocomp(
struct omap_sr *sr)
210 if (!sr_class || !(sr_class->disable)) {
212 "%s: smartreflex class driver not registered\n",
218 sr_class->disable(sr, 1);
234 static int sr_late_init(
struct omap_sr *sr_info)
236 struct omap_sr_data *
pdata = sr_info->
pdev->dev.platform_data;
240 if (sr_class->notify && sr_class->notify_flags && sr_info->
irq) {
242 0, sr_info->
name, sr_info);
248 if (pdata && pdata->enable_on_init)
249 sr_start_vddautocomp(sr_info);
258 dev_err(&sr_info->
pdev->dev,
"%s: ERROR in registering"
259 "interrupt handler. Smartreflex will"
260 "not function as desired\n", __func__);
266 static void sr_v1_disable(
struct omap_sr *sr)
296 dev_warn(&sr->
pdev->dev,
"%s: Smartreflex disable timedout\n",
304 static void sr_v2_disable(
struct omap_sr *sr)
321 ERRCONFIG_VPBOUNDINTST_V2);
336 sr_test_cond_timeout((sr_read_reg(sr,
IRQSTATUS) &
341 dev_warn(&sr->
pdev->dev,
"%s: Smartreflex disable timedout\n",
349 static struct omap_sr_nvalue_table *sr_retrieve_nvalue_row(
355 dev_warn(&sr->
pdev->dev,
"%s: Missing ntarget value table\n",
384 u32 sr_config, sr_errconfig, errconfig_offs;
385 u32 vpboundint_en, vpboundint_st;
386 u32 senp_en = 0, senn_en = 0;
387 u8 senp_shift, senn_shift;
388 struct omap_sr *sr = _sr_lookup(voltdm);
391 pr_warning(
"%s: omap_sr struct for voltdm not found\n", __func__);
396 sr_set_clk_length(sr);
421 dev_err(&sr->
pdev->dev,
"%s: Trying to Configure smartreflex"
422 "module without specifying the ip\n", __func__);
426 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
427 sr_write_reg(sr,
SRCONFIG, sr_config);
436 sr_modify_reg(sr, errconfig_offs, (vpboundint_en | vpboundint_st),
454 u32 vpboundint_en, vpboundint_st;
455 struct omap_sr *sr = _sr_lookup(voltdm);
458 pr_warning(
"%s: omap_sr struct for voltdm not found\n", __func__);
474 dev_err(&sr->
pdev->dev,
"%s: Trying to Configure smartreflex"
475 "module without specifying the ip\n", __func__);
480 sr_modify_reg(sr, errconfig_offs, vpboundint_en | vpboundint_st, 0);
502 u32 sr_config, sr_avgwt;
503 u32 senp_en = 0, senn_en = 0;
504 u8 senp_shift, senn_shift;
505 struct omap_sr *sr = _sr_lookup(voltdm);
508 pr_warning(
"%s: omap_sr struct for voltdm not found\n", __func__);
513 sr_set_clk_length(sr);
533 dev_err(&sr->
pdev->dev,
"%s: Trying to Configure smartreflex"
534 "module without specifying the ip\n", __func__);
538 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
539 sr_write_reg(sr,
SRCONFIG, sr_config);
566 dev_err(&sr->
pdev->dev,
"%s: Trying to Configure smartreflex"
567 "module without specifying the ip\n", __func__);
588 struct omap_sr *sr = _sr_lookup(voltdm);
589 struct omap_sr_nvalue_table *nvalue_row;
593 pr_warning(
"%s: omap_sr struct for voltdm not found\n", __func__);
599 if (IS_ERR(volt_data)) {
600 dev_warn(&sr->
pdev->dev,
"%s: Unable to get voltage table"
601 "for nominal voltage %ld\n", __func__, volt);
602 return PTR_ERR(volt_data);
605 nvalue_row = sr_retrieve_nvalue_row(sr, volt_data->
sr_efuse_offs);
608 dev_warn(&sr->
pdev->dev,
"%s: failure getting SR data for this voltage %ld\n",
616 pm_runtime_get_sync(&sr->
pdev->dev);
623 ret = sr_class->configure(sr);
630 sr_modify_reg(sr,
SRCONFIG, SRCONFIG_SRENABLE, SRCONFIG_SRENABLE);
643 struct omap_sr *sr = _sr_lookup(voltdm);
646 pr_warning(
"%s: omap_sr struct for voltdm not found\n", __func__);
651 if (pm_runtime_suspended(&sr->
pdev->dev))
672 pm_runtime_put_sync_suspend(&sr->
pdev->dev);
688 pr_warning(
"%s:, Smartreflex class data passed is NULL\n",
694 pr_warning(
"%s: Smartreflex class driver already registered\n",
699 sr_class = class_data;
706 sr_late_init(sr_info);
723 struct omap_sr *sr = _sr_lookup(voltdm);
726 pr_warning(
"%s: omap_sr struct for voltdm not found\n", __func__);
733 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
734 dev_warn(&sr->
pdev->dev,
"%s: smartreflex class driver not"
735 "registered\n", __func__);
739 sr_class->enable(sr);
755 struct omap_sr *sr = _sr_lookup(voltdm);
758 pr_warning(
"%s: omap_sr struct for voltdm not found\n", __func__);
765 if (!sr_class || !(sr_class->disable)) {
766 dev_warn(&sr->
pdev->dev,
"%s: smartreflex class driver not"
767 "registered\n", __func__);
771 sr_class->disable(sr, 0);
787 struct omap_sr *sr = _sr_lookup(voltdm);
790 pr_warning(
"%s: omap_sr struct for voltdm not found\n", __func__);
797 if (!sr_class || !(sr_class->disable)) {
798 dev_warn(&sr->
pdev->dev,
"%s: smartreflex class driver not"
799 "registered\n", __func__);
803 sr_class->disable(sr, 1);
817 pr_warning(
"%s: Trying to register NULL PMIC data structure"
818 "with smartreflex\n", __func__);
822 sr_pmic_data = pmic_data;
826 static int omap_sr_autocomp_show(
void *data,
u64 *
val)
831 pr_warning(
"%s: omap_sr struct not found\n", __func__);
840 static int omap_sr_autocomp_store(
void *data,
u64 val)
845 pr_warning(
"%s: omap_sr struct not found\n", __func__);
851 pr_warning(
"%s: Invalid argument %lld\n", __func__, val);
858 sr_stop_vddautocomp(sr_info);
860 sr_start_vddautocomp(sr_info);
867 omap_sr_autocomp_store,
"%llu\n");
872 struct omap_sr_data *pdata = pdev->
dev.platform_data;
874 struct dentry *nvalue_dir;
879 dev_err(&pdev->
dev,
"%s: unable to allocate sr_info\n",
884 platform_set_drvdata(pdev, sr_info);
887 dev_err(&pdev->
dev,
"%s: platform data missing\n", __func__);
889 goto err_free_devinfo;
894 dev_err(&pdev->
dev,
"%s: no mem resource\n", __func__);
896 goto err_free_devinfo;
900 dev_name(&pdev->
dev));
902 dev_err(&pdev->
dev,
"%s: no mem region\n", __func__);
904 goto err_free_devinfo;
913 if (!sr_info->
name) {
914 dev_err(&pdev->
dev,
"%s: Unable to alloc SR instance name\n",
917 goto err_release_region;
920 sr_info->
pdev = pdev;
922 sr_info->
voltdm = pdata->voltdm;
925 sr_info->
senn_mod = pdata->senn_mod;
926 sr_info->
senp_mod = pdata->senp_mod;
928 sr_info->
ip_type = pdata->ip_type;
930 if (!sr_info->
base) {
931 dev_err(&pdev->
dev,
"%s: ioremap fail\n", __func__);
939 sr_set_clk_length(sr_info);
940 sr_set_regfields(sr_info);
942 list_add(&sr_info->
node, &sr_list);
949 ret = sr_late_init(sr_info);
951 pr_warning(
"%s: Error in SR late init\n", __func__);
956 dev_info(&pdev->
dev,
"%s: SmartReflex driver initialized\n", __func__);
959 if (IS_ERR_OR_NULL(sr_dbg_dir)) {
960 ret = PTR_ERR(sr_dbg_dir);
961 pr_err(
"%s:sr debugfs dir creation failed(%d)\n",
968 if (IS_ERR_OR_NULL(sr_info->
dbg_dir)) {
969 dev_err(&pdev->
dev,
"%s: Unable to create debugfs directory\n",
971 ret = PTR_ERR(sr_info->
dbg_dir);
976 sr_info->
dbg_dir, (
void *)sr_info, &pm_sr_fops);
983 if (IS_ERR_OR_NULL(nvalue_dir)) {
984 dev_err(&pdev->
dev,
"%s: Unable to create debugfs directory"
985 "for n-values\n", __func__);
986 ret = PTR_ERR(nvalue_dir);
991 dev_warn(&pdev->
dev,
"%s: %s: No Voltage table for the corresponding vdd. Cannot create debugfs entries for n-values\n",
992 __func__, sr_info->
name);
1001 snprintf(name,
sizeof(name),
"volt_%lu",
1005 snprintf(name,
sizeof(name),
"errminlimit_%lu",
1031 struct omap_sr_data *pdata = pdev->
dev.platform_data;
1036 dev_err(&pdev->
dev,
"%s: platform data missing\n", __func__);
1040 sr_info = _sr_lookup(pdata->voltdm);
1041 if (IS_ERR(sr_info)) {
1042 dev_warn(&pdev->
dev,
"%s: omap_sr struct not found\n",
1044 return PTR_ERR(sr_info);
1048 sr_stop_vddautocomp(sr_info);
1064 struct omap_sr_data *pdata = pdev->
dev.platform_data;
1068 dev_err(&pdev->
dev,
"%s: platform data missing\n", __func__);
1072 sr_info = _sr_lookup(pdata->voltdm);
1073 if (IS_ERR(sr_info)) {
1074 dev_warn(&pdev->
dev,
"%s: omap_sr struct not found\n",
1080 sr_stop_vddautocomp(sr_info);
1089 .name =
"smartreflex",
1093 static int __init sr_init(
void)
1103 if (sr_pmic_data && sr_pmic_data->sr_pmic_init)
1104 sr_pmic_data->sr_pmic_init();
1106 pr_warning(
"%s: No PMIC hook to init smartreflex\n", __func__);
1110 pr_err(
"%s: platform driver register failed for SR\n",
1119 static void __exit sr_exit(
void)