2 * OMAP SmartReflex Voltage Control
4 * Author: Thara Gopinath <thara@ti.com>
6 * Copyright (C) 2012 Texas Instruments, Inc.
7 * Thara Gopinath <thara@ti.com>
9 * Copyright (C) 2008 Nokia Corporation
12 * Copyright (C) 2007 Texas Instruments, Inc.
13 * Lesly A M <x0080970@ti.com>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License version 2 as
17 * published by the Free Software Foundation.
20 #include <linux/module.h>
21 #include <linux/mod_devicetable.h>
22 #include <linux/interrupt.h>
23 #include <linux/clk.h>
25 #include <linux/debugfs.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/power/smartreflex.h>
31 #define DRIVER_NAME "smartreflex"
32 #define SMARTREFLEX_NAME_LEN 32
33 #define NVALUE_NAME_LEN 40
34 #define SR_DISABLE_TIMEOUT 200
36 /* sr_list contains all the instances of smartreflex module */
37 static LIST_HEAD(sr_list);
39 static struct omap_sr_class_data *sr_class;
40 static struct dentry *sr_dbg_dir;
42 static inline void sr_write_reg(struct omap_sr *sr, unsigned offset, u32 value)
44 __raw_writel(value, (sr->base + offset));
47 static inline void sr_modify_reg(struct omap_sr *sr, unsigned offset, u32 mask,
53 * Smartreflex error config register is special as it contains
54 * certain status bits which if written a 1 into means a clear
55 * of those bits. So in order to make sure no accidental write of
56 * 1 happens to those status bits, do a clear of them in the read
57 * value. This mean this API doesn't rewrite values in these bits
58 * if they are currently set, but does allow the caller to write
61 if (sr->ip_type == SR_TYPE_V1 && offset == ERRCONFIG_V1)
62 mask |= ERRCONFIG_STATUS_V1_MASK;
63 else if (sr->ip_type == SR_TYPE_V2 && offset == ERRCONFIG_V2)
64 mask |= ERRCONFIG_VPBOUNDINTST_V2;
66 reg_val = __raw_readl(sr->base + offset);
73 __raw_writel(reg_val, (sr->base + offset));
76 static inline u32 sr_read_reg(struct omap_sr *sr, unsigned offset)
78 return __raw_readl(sr->base + offset);
81 static struct omap_sr *_sr_lookup(struct voltagedomain *voltdm)
83 struct omap_sr *sr_info;
86 pr_err("%s: Null voltage domain passed!\n", __func__);
87 return ERR_PTR(-EINVAL);
90 list_for_each_entry(sr_info, &sr_list, node) {
91 if (voltdm == sr_info->voltdm)
95 return ERR_PTR(-ENODATA);
98 static irqreturn_t sr_interrupt(int irq, void *data)
100 struct omap_sr *sr_info = data;
103 switch (sr_info->ip_type) {
105 /* Read the status bits */
106 status = sr_read_reg(sr_info, ERRCONFIG_V1);
108 /* Clear them by writing back */
109 sr_write_reg(sr_info, ERRCONFIG_V1, status);
112 /* Read the status bits */
113 status = sr_read_reg(sr_info, IRQSTATUS);
115 /* Clear them by writing back */
116 sr_write_reg(sr_info, IRQSTATUS, status);
119 dev_err(&sr_info->pdev->dev, "UNKNOWN IP type %d\n",
124 if (sr_class->notify)
125 sr_class->notify(sr_info, status);
130 static void sr_set_clk_length(struct omap_sr *sr)
135 /* Try interconnect target module fck first if it already exists */
136 fck = clk_get(sr->pdev->dev.parent, "fck");
138 fck = clk_get(&sr->pdev->dev, "fck");
140 dev_err(&sr->pdev->dev,
141 "%s: unable to get fck for device %s\n",
142 __func__, dev_name(&sr->pdev->dev));
147 fclk_speed = clk_get_rate(fck);
150 switch (fclk_speed) {
152 sr->clk_length = SRCLKLENGTH_12MHZ_SYSCLK;
155 sr->clk_length = SRCLKLENGTH_13MHZ_SYSCLK;
158 sr->clk_length = SRCLKLENGTH_19MHZ_SYSCLK;
161 sr->clk_length = SRCLKLENGTH_26MHZ_SYSCLK;
164 sr->clk_length = SRCLKLENGTH_38MHZ_SYSCLK;
167 dev_err(&sr->pdev->dev, "%s: Invalid fclk rate: %d\n",
168 __func__, fclk_speed);
173 static void sr_start_vddautocomp(struct omap_sr *sr)
175 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
176 dev_warn(&sr->pdev->dev,
177 "%s: smartreflex class driver not registered\n",
182 if (!sr_class->enable(sr))
183 sr->autocomp_active = true;
186 static void sr_stop_vddautocomp(struct omap_sr *sr)
188 if (!sr_class || !(sr_class->disable)) {
189 dev_warn(&sr->pdev->dev,
190 "%s: smartreflex class driver not registered\n",
195 if (sr->autocomp_active) {
196 sr_class->disable(sr, 1);
197 sr->autocomp_active = false;
202 * This function handles the initializations which have to be done
203 * only when both sr device and class driver regiter has
204 * completed. This will be attempted to be called from both sr class
205 * driver register and sr device intializtion API's. Only one call
206 * will ultimately succeed.
208 * Currently this function registers interrupt handler for a particular SR
209 * if smartreflex class driver is already registered and has
210 * requested for interrupts and the SR interrupt line in present.
212 static int sr_late_init(struct omap_sr *sr_info)
214 struct omap_sr_data *pdata = sr_info->pdev->dev.platform_data;
217 if (sr_class->notify && sr_class->notify_flags && sr_info->irq) {
218 ret = devm_request_irq(&sr_info->pdev->dev, sr_info->irq,
219 sr_interrupt, 0, sr_info->name, sr_info);
222 disable_irq(sr_info->irq);
225 if (pdata && pdata->enable_on_init)
226 sr_start_vddautocomp(sr_info);
231 list_del(&sr_info->node);
232 dev_err(&sr_info->pdev->dev, "%s: ERROR in registering interrupt handler. Smartreflex will not function as desired\n",
238 static void sr_v1_disable(struct omap_sr *sr)
241 int errconf_val = ERRCONFIG_MCUACCUMINTST | ERRCONFIG_MCUVALIDINTST |
242 ERRCONFIG_MCUBOUNDINTST;
244 /* Enable MCUDisableAcknowledge interrupt */
245 sr_modify_reg(sr, ERRCONFIG_V1,
246 ERRCONFIG_MCUDISACKINTEN, ERRCONFIG_MCUDISACKINTEN);
248 /* SRCONFIG - disable SR */
249 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
251 /* Disable all other SR interrupts and clear the status as needed */
252 if (sr_read_reg(sr, ERRCONFIG_V1) & ERRCONFIG_VPBOUNDINTST_V1)
253 errconf_val |= ERRCONFIG_VPBOUNDINTST_V1;
254 sr_modify_reg(sr, ERRCONFIG_V1,
255 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
256 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_VPBOUNDINTEN_V1),
260 * Wait for SR to be disabled.
261 * wait until ERRCONFIG.MCUDISACKINTST = 1. Typical latency is 1us.
263 sr_test_cond_timeout((sr_read_reg(sr, ERRCONFIG_V1) &
264 ERRCONFIG_MCUDISACKINTST), SR_DISABLE_TIMEOUT,
267 if (timeout >= SR_DISABLE_TIMEOUT)
268 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
271 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
272 sr_modify_reg(sr, ERRCONFIG_V1, ERRCONFIG_MCUDISACKINTEN,
273 ERRCONFIG_MCUDISACKINTST);
276 static void sr_v2_disable(struct omap_sr *sr)
280 /* Enable MCUDisableAcknowledge interrupt */
281 sr_write_reg(sr, IRQENABLE_SET, IRQENABLE_MCUDISABLEACKINT);
283 /* SRCONFIG - disable SR */
284 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, 0x0);
287 * Disable all other SR interrupts and clear the status
288 * write to status register ONLY on need basis - only if status
291 if (sr_read_reg(sr, ERRCONFIG_V2) & ERRCONFIG_VPBOUNDINTST_V2)
292 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
293 ERRCONFIG_VPBOUNDINTST_V2);
295 sr_modify_reg(sr, ERRCONFIG_V2, ERRCONFIG_VPBOUNDINTEN_V2,
297 sr_write_reg(sr, IRQENABLE_CLR, (IRQENABLE_MCUACCUMINT |
298 IRQENABLE_MCUVALIDINT |
299 IRQENABLE_MCUBOUNDSINT));
300 sr_write_reg(sr, IRQSTATUS, (IRQSTATUS_MCUACCUMINT |
301 IRQSTATUS_MCVALIDINT |
302 IRQSTATUS_MCBOUNDSINT));
305 * Wait for SR to be disabled.
306 * wait until IRQSTATUS.MCUDISACKINTST = 1. Typical latency is 1us.
308 sr_test_cond_timeout((sr_read_reg(sr, IRQSTATUS) &
309 IRQSTATUS_MCUDISABLEACKINT), SR_DISABLE_TIMEOUT,
312 if (timeout >= SR_DISABLE_TIMEOUT)
313 dev_warn(&sr->pdev->dev, "%s: Smartreflex disable timedout\n",
316 /* Disable MCUDisableAcknowledge interrupt & clear pending interrupt */
317 sr_write_reg(sr, IRQENABLE_CLR, IRQENABLE_MCUDISABLEACKINT);
318 sr_write_reg(sr, IRQSTATUS, IRQSTATUS_MCUDISABLEACKINT);
321 static struct omap_sr_nvalue_table *sr_retrieve_nvalue_row(
322 struct omap_sr *sr, u32 efuse_offs)
326 if (!sr->nvalue_table) {
327 dev_warn(&sr->pdev->dev, "%s: Missing ntarget value table\n",
332 for (i = 0; i < sr->nvalue_count; i++) {
333 if (sr->nvalue_table[i].efuse_offs == efuse_offs)
334 return &sr->nvalue_table[i];
340 /* Public Functions */
343 * sr_configure_errgen() - Configures the SmartReflex to perform AVS using the
344 * error generator module.
345 * @sr: SR module to be configured.
347 * This API is to be called from the smartreflex class driver to
348 * configure the error generator module inside the smartreflex module.
349 * SR settings if using the ERROR module inside Smartreflex.
350 * SR CLASS 3 by default uses only the ERROR module where as
351 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
352 * module. Returns 0 on success and error value in case of failure.
354 int sr_configure_errgen(struct omap_sr *sr)
356 u32 sr_config, sr_errconfig, errconfig_offs;
357 u32 vpboundint_en, vpboundint_st;
358 u32 senp_en = 0, senn_en = 0;
359 u8 senp_shift, senn_shift;
362 pr_warn("%s: NULL omap_sr from %pS\n",
363 __func__, (void *)_RET_IP_);
368 sr_set_clk_length(sr);
370 senp_en = sr->senp_mod;
371 senn_en = sr->senn_mod;
373 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
374 SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN;
376 switch (sr->ip_type) {
378 sr_config |= SRCONFIG_DELAYCTRL;
379 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
380 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
381 errconfig_offs = ERRCONFIG_V1;
382 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
383 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
386 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
387 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
388 errconfig_offs = ERRCONFIG_V2;
389 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
390 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
393 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
398 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
399 sr_write_reg(sr, SRCONFIG, sr_config);
400 sr_errconfig = (sr->err_weight << ERRCONFIG_ERRWEIGHT_SHIFT) |
401 (sr->err_maxlimit << ERRCONFIG_ERRMAXLIMIT_SHIFT) |
402 (sr->err_minlimit << ERRCONFIG_ERRMINLIMIT_SHIFT);
403 sr_modify_reg(sr, errconfig_offs, (SR_ERRWEIGHT_MASK |
404 SR_ERRMAXLIMIT_MASK | SR_ERRMINLIMIT_MASK),
407 /* Enabling the interrupts if the ERROR module is used */
408 sr_modify_reg(sr, errconfig_offs, (vpboundint_en | vpboundint_st),
415 * sr_disable_errgen() - Disables SmartReflex AVS module's errgen component
416 * @sr: SR module to be configured.
418 * This API is to be called from the smartreflex class driver to
419 * disable the error generator module inside the smartreflex module.
421 * Returns 0 on success and error value in case of failure.
423 int sr_disable_errgen(struct omap_sr *sr)
426 u32 vpboundint_en, vpboundint_st;
429 pr_warn("%s: NULL omap_sr from %pS\n",
430 __func__, (void *)_RET_IP_);
434 switch (sr->ip_type) {
436 errconfig_offs = ERRCONFIG_V1;
437 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V1;
438 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V1;
441 errconfig_offs = ERRCONFIG_V2;
442 vpboundint_en = ERRCONFIG_VPBOUNDINTEN_V2;
443 vpboundint_st = ERRCONFIG_VPBOUNDINTST_V2;
446 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
451 /* Disable the Sensor and errorgen */
452 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SENENABLE | SRCONFIG_ERRGEN_EN, 0);
455 * Disable the interrupts of ERROR module
456 * NOTE: modify is a read, modify,write - an implicit OCP barrier
457 * which is required is present here - sequencing is critical
458 * at this point (after errgen is disabled, vpboundint disable)
460 sr_modify_reg(sr, errconfig_offs, vpboundint_en | vpboundint_st, 0);
466 * sr_configure_minmax() - Configures the SmartReflex to perform AVS using the
468 * @sr: SR module to be configured.
470 * This API is to be called from the smartreflex class driver to
471 * configure the minmaxavg module inside the smartreflex module.
472 * SR settings if using the ERROR module inside Smartreflex.
473 * SR CLASS 3 by default uses only the ERROR module where as
474 * SR CLASS 2 can choose between ERROR module and MINMAXAVG
475 * module. Returns 0 on success and error value in case of failure.
477 int sr_configure_minmax(struct omap_sr *sr)
479 u32 sr_config, sr_avgwt;
480 u32 senp_en = 0, senn_en = 0;
481 u8 senp_shift, senn_shift;
484 pr_warn("%s: NULL omap_sr from %pS\n",
485 __func__, (void *)_RET_IP_);
490 sr_set_clk_length(sr);
492 senp_en = sr->senp_mod;
493 senn_en = sr->senn_mod;
495 sr_config = (sr->clk_length << SRCONFIG_SRCLKLENGTH_SHIFT) |
497 (sr->accum_data << SRCONFIG_ACCUMDATA_SHIFT);
499 switch (sr->ip_type) {
501 sr_config |= SRCONFIG_DELAYCTRL;
502 senn_shift = SRCONFIG_SENNENABLE_V1_SHIFT;
503 senp_shift = SRCONFIG_SENPENABLE_V1_SHIFT;
506 senn_shift = SRCONFIG_SENNENABLE_V2_SHIFT;
507 senp_shift = SRCONFIG_SENPENABLE_V2_SHIFT;
510 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
515 sr_config |= ((senn_en << senn_shift) | (senp_en << senp_shift));
516 sr_write_reg(sr, SRCONFIG, sr_config);
517 sr_avgwt = (sr->senp_avgweight << AVGWEIGHT_SENPAVGWEIGHT_SHIFT) |
518 (sr->senn_avgweight << AVGWEIGHT_SENNAVGWEIGHT_SHIFT);
519 sr_write_reg(sr, AVGWEIGHT, sr_avgwt);
522 * Enabling the interrupts if MINMAXAVG module is used.
523 * TODO: check if all the interrupts are mandatory
525 switch (sr->ip_type) {
527 sr_modify_reg(sr, ERRCONFIG_V1,
528 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUVALIDINTEN |
529 ERRCONFIG_MCUBOUNDINTEN),
530 (ERRCONFIG_MCUACCUMINTEN | ERRCONFIG_MCUACCUMINTST |
531 ERRCONFIG_MCUVALIDINTEN | ERRCONFIG_MCUVALIDINTST |
532 ERRCONFIG_MCUBOUNDINTEN | ERRCONFIG_MCUBOUNDINTST));
535 sr_write_reg(sr, IRQSTATUS,
536 IRQSTATUS_MCUACCUMINT | IRQSTATUS_MCVALIDINT |
537 IRQSTATUS_MCBOUNDSINT | IRQSTATUS_MCUDISABLEACKINT);
538 sr_write_reg(sr, IRQENABLE_SET,
539 IRQENABLE_MCUACCUMINT | IRQENABLE_MCUVALIDINT |
540 IRQENABLE_MCUBOUNDSINT | IRQENABLE_MCUDISABLEACKINT);
543 dev_err(&sr->pdev->dev, "%s: Trying to Configure smartreflex module without specifying the ip\n",
552 * sr_enable() - Enables the smartreflex module.
553 * @sr: pointer to which the SR module to be configured belongs to.
554 * @volt: The voltage at which the Voltage domain associated with
555 * the smartreflex module is operating at.
556 * This is required only to program the correct Ntarget value.
558 * This API is to be called from the smartreflex class driver to
559 * enable a smartreflex module. Returns 0 on success. Returns error
560 * value if the voltage passed is wrong or if ntarget value is wrong.
562 int sr_enable(struct omap_sr *sr, unsigned long volt)
564 struct omap_volt_data *volt_data;
565 struct omap_sr_nvalue_table *nvalue_row;
569 pr_warn("%s: NULL omap_sr from %pS\n",
570 __func__, (void *)_RET_IP_);
574 volt_data = omap_voltage_get_voltdata(sr->voltdm, volt);
576 if (IS_ERR(volt_data)) {
577 dev_warn(&sr->pdev->dev, "%s: Unable to get voltage table for nominal voltage %ld\n",
579 return PTR_ERR(volt_data);
582 nvalue_row = sr_retrieve_nvalue_row(sr, volt_data->sr_efuse_offs);
585 dev_warn(&sr->pdev->dev, "%s: failure getting SR data for this voltage %ld\n",
590 /* errminlimit is opp dependent and hence linked to voltage */
591 sr->err_minlimit = nvalue_row->errminlimit;
593 pm_runtime_get_sync(&sr->pdev->dev);
595 /* Check if SR is already enabled. If yes do nothing */
596 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE)
600 ret = sr_class->configure(sr);
604 sr_write_reg(sr, NVALUERECIPROCAL, nvalue_row->nvalue);
606 /* SRCONFIG - enable SR */
607 sr_modify_reg(sr, SRCONFIG, SRCONFIG_SRENABLE, SRCONFIG_SRENABLE);
612 * sr_disable() - Disables the smartreflex module.
613 * @sr: pointer to which the SR module to be configured belongs to.
615 * This API is to be called from the smartreflex class driver to
616 * disable a smartreflex module.
618 void sr_disable(struct omap_sr *sr)
621 pr_warn("%s: NULL omap_sr from %pS\n",
622 __func__, (void *)_RET_IP_);
626 /* Check if SR clocks are already disabled. If yes do nothing */
627 if (pm_runtime_suspended(&sr->pdev->dev))
631 * Disable SR if only it is indeed enabled. Else just
632 * disable the clocks.
634 if (sr_read_reg(sr, SRCONFIG) & SRCONFIG_SRENABLE) {
635 switch (sr->ip_type) {
643 dev_err(&sr->pdev->dev, "UNKNOWN IP type %d\n",
648 pm_runtime_put_sync_suspend(&sr->pdev->dev);
652 * sr_register_class() - API to register a smartreflex class parameters.
653 * @class_data: The structure containing various sr class specific data.
655 * This API is to be called by the smartreflex class driver to register itself
656 * with the smartreflex driver during init. Returns 0 on success else the
659 int sr_register_class(struct omap_sr_class_data *class_data)
661 struct omap_sr *sr_info;
664 pr_warn("%s:, Smartreflex class data passed is NULL\n",
670 pr_warn("%s: Smartreflex class driver already registered\n",
675 sr_class = class_data;
678 * Call into late init to do initializations that require
679 * both sr driver and sr class driver to be initiallized.
681 list_for_each_entry(sr_info, &sr_list, node)
682 sr_late_init(sr_info);
688 * omap_sr_enable() - API to enable SR clocks and to call into the
689 * registered smartreflex class enable API.
690 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
692 * This API is to be called from the kernel in order to enable
693 * a particular smartreflex module. This API will do the initial
694 * configurations to turn on the smartreflex module and in turn call
695 * into the registered smartreflex class enable API.
697 void omap_sr_enable(struct voltagedomain *voltdm)
699 struct omap_sr *sr = _sr_lookup(voltdm);
702 pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
706 if (!sr->autocomp_active)
709 if (!sr_class || !(sr_class->enable) || !(sr_class->configure)) {
710 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
715 sr_class->enable(sr);
719 * omap_sr_disable() - API to disable SR without resetting the voltage
721 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
723 * This API is to be called from the kernel in order to disable
724 * a particular smartreflex module. This API will in turn call
725 * into the registered smartreflex class disable API. This API will tell
726 * the smartreflex class disable not to reset the VP voltage after
727 * disabling smartreflex.
729 void omap_sr_disable(struct voltagedomain *voltdm)
731 struct omap_sr *sr = _sr_lookup(voltdm);
734 pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
738 if (!sr->autocomp_active)
741 if (!sr_class || !(sr_class->disable)) {
742 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
747 sr_class->disable(sr, 0);
751 * omap_sr_disable_reset_volt() - API to disable SR and reset the
752 * voltage processor voltage
753 * @voltdm: VDD pointer to which the SR module to be configured belongs to.
755 * This API is to be called from the kernel in order to disable
756 * a particular smartreflex module. This API will in turn call
757 * into the registered smartreflex class disable API. This API will tell
758 * the smartreflex class disable to reset the VP voltage after
759 * disabling smartreflex.
761 void omap_sr_disable_reset_volt(struct voltagedomain *voltdm)
763 struct omap_sr *sr = _sr_lookup(voltdm);
766 pr_warn("%s: omap_sr struct for voltdm not found\n", __func__);
770 if (!sr->autocomp_active)
773 if (!sr_class || !(sr_class->disable)) {
774 dev_warn(&sr->pdev->dev, "%s: smartreflex class driver not registered\n",
779 sr_class->disable(sr, 1);
782 /* PM Debug FS entries to enable and disable smartreflex. */
783 static int omap_sr_autocomp_show(void *data, u64 *val)
785 struct omap_sr *sr_info = data;
788 pr_warn("%s: omap_sr struct not found\n", __func__);
792 *val = sr_info->autocomp_active;
797 static int omap_sr_autocomp_store(void *data, u64 val)
799 struct omap_sr *sr_info = data;
802 pr_warn("%s: omap_sr struct not found\n", __func__);
808 pr_warn("%s: Invalid argument %lld\n", __func__, val);
812 /* control enable/disable only if there is a delta in value */
813 if (sr_info->autocomp_active != val) {
815 sr_stop_vddautocomp(sr_info);
817 sr_start_vddautocomp(sr_info);
823 DEFINE_SIMPLE_ATTRIBUTE(pm_sr_fops, omap_sr_autocomp_show,
824 omap_sr_autocomp_store, "%llu\n");
826 static int omap_sr_probe(struct platform_device *pdev)
828 struct omap_sr *sr_info;
829 struct omap_sr_data *pdata = pdev->dev.platform_data;
830 struct resource *mem, *irq;
831 struct dentry *nvalue_dir;
834 sr_info = devm_kzalloc(&pdev->dev, sizeof(struct omap_sr), GFP_KERNEL);
838 sr_info->name = devm_kzalloc(&pdev->dev,
839 SMARTREFLEX_NAME_LEN, GFP_KERNEL);
843 platform_set_drvdata(pdev, sr_info);
846 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
850 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
851 sr_info->base = devm_ioremap_resource(&pdev->dev, mem);
852 if (IS_ERR(sr_info->base)) {
853 dev_err(&pdev->dev, "%s: ioremap fail\n", __func__);
854 return PTR_ERR(sr_info->base);
857 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
859 pm_runtime_enable(&pdev->dev);
860 pm_runtime_irq_safe(&pdev->dev);
862 snprintf(sr_info->name, SMARTREFLEX_NAME_LEN, "%s", pdata->name);
864 sr_info->pdev = pdev;
865 sr_info->srid = pdev->id;
866 sr_info->voltdm = pdata->voltdm;
867 sr_info->nvalue_table = pdata->nvalue_table;
868 sr_info->nvalue_count = pdata->nvalue_count;
869 sr_info->senn_mod = pdata->senn_mod;
870 sr_info->senp_mod = pdata->senp_mod;
871 sr_info->err_weight = pdata->err_weight;
872 sr_info->err_maxlimit = pdata->err_maxlimit;
873 sr_info->accum_data = pdata->accum_data;
874 sr_info->senn_avgweight = pdata->senn_avgweight;
875 sr_info->senp_avgweight = pdata->senp_avgweight;
876 sr_info->autocomp_active = false;
877 sr_info->ip_type = pdata->ip_type;
880 sr_info->irq = irq->start;
882 sr_set_clk_length(sr_info);
884 list_add(&sr_info->node, &sr_list);
886 ret = pm_runtime_get_sync(&pdev->dev);
888 pm_runtime_put_noidle(&pdev->dev);
893 * Call into late init to do initializations that require
894 * both sr driver and sr class driver to be initiallized.
897 ret = sr_late_init(sr_info);
899 pr_warn("%s: Error in SR late init\n", __func__);
904 dev_info(&pdev->dev, "%s: SmartReflex driver initialized\n", __func__);
906 sr_dbg_dir = debugfs_create_dir("smartreflex", NULL);
907 if (IS_ERR_OR_NULL(sr_dbg_dir)) {
908 ret = PTR_ERR(sr_dbg_dir);
909 pr_err("%s:sr debugfs dir creation failed(%d)\n",
915 sr_info->dbg_dir = debugfs_create_dir(sr_info->name, sr_dbg_dir);
916 if (IS_ERR_OR_NULL(sr_info->dbg_dir)) {
917 dev_err(&pdev->dev, "%s: Unable to create debugfs directory\n",
919 ret = PTR_ERR(sr_info->dbg_dir);
923 (void) debugfs_create_file("autocomp", S_IRUGO | S_IWUSR,
924 sr_info->dbg_dir, (void *)sr_info, &pm_sr_fops);
925 (void) debugfs_create_x32("errweight", S_IRUGO, sr_info->dbg_dir,
926 &sr_info->err_weight);
927 (void) debugfs_create_x32("errmaxlimit", S_IRUGO, sr_info->dbg_dir,
928 &sr_info->err_maxlimit);
930 nvalue_dir = debugfs_create_dir("nvalue", sr_info->dbg_dir);
931 if (IS_ERR_OR_NULL(nvalue_dir)) {
932 dev_err(&pdev->dev, "%s: Unable to create debugfs directory for n-values\n",
934 ret = PTR_ERR(nvalue_dir);
938 if (sr_info->nvalue_count == 0 || !sr_info->nvalue_table) {
939 dev_warn(&pdev->dev, "%s: %s: No Voltage table for the corresponding vdd. Cannot create debugfs entries for n-values\n",
940 __func__, sr_info->name);
946 for (i = 0; i < sr_info->nvalue_count; i++) {
947 char name[NVALUE_NAME_LEN + 1];
949 snprintf(name, sizeof(name), "volt_%lu",
950 sr_info->nvalue_table[i].volt_nominal);
951 (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
952 &(sr_info->nvalue_table[i].nvalue));
953 snprintf(name, sizeof(name), "errminlimit_%lu",
954 sr_info->nvalue_table[i].volt_nominal);
955 (void) debugfs_create_x32(name, S_IRUGO | S_IWUSR, nvalue_dir,
956 &(sr_info->nvalue_table[i].errminlimit));
960 pm_runtime_put_sync(&pdev->dev);
965 debugfs_remove_recursive(sr_info->dbg_dir);
967 list_del(&sr_info->node);
969 pm_runtime_put_sync(&pdev->dev);
974 static int omap_sr_remove(struct platform_device *pdev)
976 struct omap_sr_data *pdata = pdev->dev.platform_data;
977 struct omap_sr *sr_info;
980 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
984 sr_info = _sr_lookup(pdata->voltdm);
985 if (IS_ERR(sr_info)) {
986 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
988 return PTR_ERR(sr_info);
991 if (sr_info->autocomp_active)
992 sr_stop_vddautocomp(sr_info);
993 if (sr_info->dbg_dir)
994 debugfs_remove_recursive(sr_info->dbg_dir);
996 pm_runtime_disable(&pdev->dev);
997 list_del(&sr_info->node);
1001 static void omap_sr_shutdown(struct platform_device *pdev)
1003 struct omap_sr_data *pdata = pdev->dev.platform_data;
1004 struct omap_sr *sr_info;
1007 dev_err(&pdev->dev, "%s: platform data missing\n", __func__);
1011 sr_info = _sr_lookup(pdata->voltdm);
1012 if (IS_ERR(sr_info)) {
1013 dev_warn(&pdev->dev, "%s: omap_sr struct not found\n",
1018 if (sr_info->autocomp_active)
1019 sr_stop_vddautocomp(sr_info);
1024 static const struct of_device_id omap_sr_match[] = {
1025 { .compatible = "ti,omap3-smartreflex-core", },
1026 { .compatible = "ti,omap3-smartreflex-mpu-iva", },
1027 { .compatible = "ti,omap4-smartreflex-core", },
1028 { .compatible = "ti,omap4-smartreflex-mpu", },
1029 { .compatible = "ti,omap4-smartreflex-iva", },
1032 MODULE_DEVICE_TABLE(of, omap_sr_match);
1034 static struct platform_driver smartreflex_driver = {
1035 .probe = omap_sr_probe,
1036 .remove = omap_sr_remove,
1037 .shutdown = omap_sr_shutdown,
1039 .name = DRIVER_NAME,
1040 .of_match_table = omap_sr_match,
1044 static int __init sr_init(void)
1048 ret = platform_driver_register(&smartreflex_driver);
1050 pr_err("%s: platform driver register failed for SR\n",
1057 late_initcall(sr_init);
1059 static void __exit sr_exit(void)
1061 platform_driver_unregister(&smartreflex_driver);
1063 module_exit(sr_exit);
1065 MODULE_DESCRIPTION("OMAP Smartreflex Driver");
1066 MODULE_LICENSE("GPL");
1067 MODULE_ALIAS("platform:" DRIVER_NAME);
1068 MODULE_AUTHOR("Texas Instruments Inc");