clock_ops.c 11.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * drivers/base/power/clock_ops.c - Generic clock manipulation PM callbacks
 *
 * Copyright (c) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
 *
 * This file is released under the GPLv2.
 */

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/io.h>
#include <linux/pm.h>
13
#include <linux/pm_clock.h>
14 15 16 17
#include <linux/clk.h>
#include <linux/slab.h>
#include <linux/err.h>

18
#ifdef CONFIG_PM
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

enum pce_status {
	PCE_STATUS_NONE = 0,
	PCE_STATUS_ACQUIRED,
	PCE_STATUS_ENABLED,
	PCE_STATUS_ERROR,
};

struct pm_clock_entry {
	struct list_head node;
	char *con_id;
	struct clk *clk;
	enum pce_status status;
};

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/**
 * pm_clk_acquire - Acquire a device clock.
 * @dev: Device whose clock is to be acquired.
 * @ce: PM clock entry corresponding to the clock.
 */
static void pm_clk_acquire(struct device *dev, struct pm_clock_entry *ce)
{
	ce->clk = clk_get(dev, ce->con_id);
	if (IS_ERR(ce->clk)) {
		ce->status = PCE_STATUS_ERROR;
	} else {
		ce->status = PCE_STATUS_ACQUIRED;
		dev_dbg(dev, "Clock %s managed by runtime PM.\n", ce->con_id);
	}
}

50
/**
51 52
 * pm_clk_add - Start using a device clock for power management.
 * @dev: Device whose clock is going to be used for power management.
53 54 55
 * @con_id: Connection ID of the clock.
 *
 * Add the clock represented by @con_id to the list of clocks used for
56
 * the power management of @dev.
57
 */
58
int pm_clk_add(struct device *dev, const char *con_id)
59
{
60
	struct pm_subsys_data *psd = dev_to_psd(dev);
61 62
	struct pm_clock_entry *ce;

63
	if (!psd)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
		return -EINVAL;

	ce = kzalloc(sizeof(*ce), GFP_KERNEL);
	if (!ce) {
		dev_err(dev, "Not enough memory for clock entry.\n");
		return -ENOMEM;
	}

	if (con_id) {
		ce->con_id = kstrdup(con_id, GFP_KERNEL);
		if (!ce->con_id) {
			dev_err(dev,
				"Not enough memory for clock connection ID.\n");
			kfree(ce);
			return -ENOMEM;
		}
	}

82 83
	pm_clk_acquire(dev, ce);

84 85 86
	spin_lock_irq(&psd->lock);
	list_add_tail(&ce->node, &psd->clock_list);
	spin_unlock_irq(&psd->lock);
87 88 89 90
	return 0;
}

/**
91 92
 * __pm_clk_remove - Destroy PM clock entry.
 * @ce: PM clock entry to destroy.
93
 */
94
static void __pm_clk_remove(struct pm_clock_entry *ce)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
{
	if (!ce)
		return;

	if (ce->status < PCE_STATUS_ERROR) {
		if (ce->status == PCE_STATUS_ENABLED)
			clk_disable(ce->clk);

		if (ce->status >= PCE_STATUS_ACQUIRED)
			clk_put(ce->clk);
	}

	if (ce->con_id)
		kfree(ce->con_id);

	kfree(ce);
}

/**
114 115
 * pm_clk_remove - Stop using a device clock for power management.
 * @dev: Device whose clock should not be used for PM any more.
116 117 118
 * @con_id: Connection ID of the clock.
 *
 * Remove the clock represented by @con_id from the list of clocks used for
119
 * the power management of @dev.
120
 */
121
void pm_clk_remove(struct device *dev, const char *con_id)
122
{
123
	struct pm_subsys_data *psd = dev_to_psd(dev);
124 125
	struct pm_clock_entry *ce;

126
	if (!psd)
127 128
		return;

129
	spin_lock_irq(&psd->lock);
130

131
	list_for_each_entry(ce, &psd->clock_list, node) {
132 133 134
		if (!con_id && !ce->con_id)
			goto remove;
		else if (!con_id || !ce->con_id)
135
			continue;
136 137
		else if (!strcmp(con_id, ce->con_id))
			goto remove;
138 139
	}

140
	spin_unlock_irq(&psd->lock);
141 142 143 144
	return;

 remove:
	list_del(&ce->node);
145
	spin_unlock_irq(&psd->lock);
146 147

	__pm_clk_remove(ce);
148 149 150
}

/**
151 152
 * pm_clk_init - Initialize a device's list of power management clocks.
 * @dev: Device to initialize the list of PM clocks for.
153
 *
154 155
 * Initialize the lock and clock_list members of the device's pm_subsys_data
 * object.
156
 */
157
void pm_clk_init(struct device *dev)
158
{
159
	struct pm_subsys_data *psd = dev_to_psd(dev);
160 161
	if (psd)
		INIT_LIST_HEAD(&psd->clock_list);
162 163 164 165 166 167 168 169 170 171 172
}

/**
 * pm_clk_create - Create and initialize a device's list of PM clocks.
 * @dev: Device to create and initialize the list of PM clocks for.
 *
 * Allocate a struct pm_subsys_data object, initialize its lock and clock_list
 * members and make the @dev's power.subsys_data field point to it.
 */
int pm_clk_create(struct device *dev)
{
173 174
	int ret = dev_pm_get_subsys_data(dev);
	return ret < 0 ? ret : 0;
175 176 177
}

/**
178 179
 * pm_clk_destroy - Destroy a device's list of power management clocks.
 * @dev: Device to destroy the list of PM clocks for.
180 181
 *
 * Clear the @dev's power.subsys_data field, remove the list of clock entries
182
 * from the struct pm_subsys_data object pointed to by it before and free
183 184
 * that object.
 */
185
void pm_clk_destroy(struct device *dev)
186
{
187
	struct pm_subsys_data *psd = dev_to_psd(dev);
188
	struct pm_clock_entry *ce, *c;
189
	struct list_head list;
190

191
	if (!psd)
192 193
		return;

194
	INIT_LIST_HEAD(&list);
195

196
	spin_lock_irq(&psd->lock);
197

198
	list_for_each_entry_safe_reverse(ce, c, &psd->clock_list, node)
199
		list_move(&ce->node, &list);
200

201
	spin_unlock_irq(&psd->lock);
202

203
	dev_pm_put_subsys_data(dev);
204 205 206 207 208

	list_for_each_entry_safe_reverse(ce, c, &list, node) {
		list_del(&ce->node);
		__pm_clk_remove(ce);
	}
209 210
}

211 212 213 214
#endif /* CONFIG_PM */

#ifdef CONFIG_PM_RUNTIME

215
/**
216
 * pm_clk_suspend - Disable clocks in a device's PM clock list.
217 218
 * @dev: Device to disable the clocks for.
 */
219
int pm_clk_suspend(struct device *dev)
220
{
221
	struct pm_subsys_data *psd = dev_to_psd(dev);
222
	struct pm_clock_entry *ce;
223
	unsigned long flags;
224 225 226

	dev_dbg(dev, "%s()\n", __func__);

227
	if (!psd)
228 229
		return 0;

230
	spin_lock_irqsave(&psd->lock, flags);
231

232
	list_for_each_entry_reverse(ce, &psd->clock_list, node) {
233 234 235 236 237 238
		if (ce->status < PCE_STATUS_ERROR) {
			clk_disable(ce->clk);
			ce->status = PCE_STATUS_ACQUIRED;
		}
	}

239
	spin_unlock_irqrestore(&psd->lock, flags);
240 241 242 243 244

	return 0;
}

/**
245
 * pm_clk_resume - Enable clocks in a device's PM clock list.
246 247
 * @dev: Device to enable the clocks for.
 */
248
int pm_clk_resume(struct device *dev)
249
{
250
	struct pm_subsys_data *psd = dev_to_psd(dev);
251
	struct pm_clock_entry *ce;
252
	unsigned long flags;
253 254 255

	dev_dbg(dev, "%s()\n", __func__);

256
	if (!psd)
257 258
		return 0;

259
	spin_lock_irqsave(&psd->lock, flags);
260

261
	list_for_each_entry(ce, &psd->clock_list, node) {
262 263 264 265 266 267
		if (ce->status < PCE_STATUS_ERROR) {
			clk_enable(ce->clk);
			ce->status = PCE_STATUS_ENABLED;
		}
	}

268
	spin_unlock_irqrestore(&psd->lock, flags);
269 270 271 272 273

	return 0;
}

/**
274
 * pm_clk_notify - Notify routine for device addition and removal.
275 276 277 278 279 280
 * @nb: Notifier block object this function is a member of.
 * @action: Operation being carried out by the caller.
 * @data: Device the routine is being run for.
 *
 * For this function to work, @nb must be a member of an object of type
 * struct pm_clk_notifier_block containing all of the requisite data.
281 282
 * Specifically, the pm_domain member of that object is copied to the device's
 * pm_domain field and its con_ids member is used to populate the device's list
283
 * of PM clocks, depending on @action.
284
 *
285
 * If the device's pm_domain field is already populated with a value different
286 287 288
 * from the one stored in the struct pm_clk_notifier_block object, the function
 * does nothing.
 */
289
static int pm_clk_notify(struct notifier_block *nb,
290 291 292 293
				 unsigned long action, void *data)
{
	struct pm_clk_notifier_block *clknb;
	struct device *dev = data;
294
	char **con_id;
295 296 297 298 299 300 301 302
	int error;

	dev_dbg(dev, "%s() %ld\n", __func__, action);

	clknb = container_of(nb, struct pm_clk_notifier_block, nb);

	switch (action) {
	case BUS_NOTIFY_ADD_DEVICE:
303
		if (dev->pm_domain)
304 305
			break;

306
		error = pm_clk_create(dev);
307 308 309
		if (error)
			break;

310
		dev->pm_domain = clknb->pm_domain;
311
		if (clknb->con_ids[0]) {
312
			for (con_id = clknb->con_ids; *con_id; con_id++)
313
				pm_clk_add(dev, *con_id);
314
		} else {
315
			pm_clk_add(dev, NULL);
316 317 318 319
		}

		break;
	case BUS_NOTIFY_DEL_DEVICE:
320
		if (dev->pm_domain != clknb->pm_domain)
321 322
			break;

323
		dev->pm_domain = NULL;
324
		pm_clk_destroy(dev);
325 326 327 328 329 330 331 332
		break;
	}

	return 0;
}

#else /* !CONFIG_PM_RUNTIME */

333 334 335
#ifdef CONFIG_PM

/**
336
 * pm_clk_suspend - Disable clocks in a device's PM clock list.
337 338
 * @dev: Device to disable the clocks for.
 */
339
int pm_clk_suspend(struct device *dev)
340
{
341
	struct pm_subsys_data *psd = dev_to_psd(dev);
342
	struct pm_clock_entry *ce;
343
	unsigned long flags;
344 345 346 347

	dev_dbg(dev, "%s()\n", __func__);

	/* If there is no driver, the clocks are already disabled. */
348
	if (!psd || !dev->driver)
349 350
		return 0;

351
	spin_lock_irqsave(&psd->lock, flags);
352

353
	list_for_each_entry_reverse(ce, &psd->clock_list, node)
354 355
		clk_disable(ce->clk);

356
	spin_unlock_irqrestore(&psd->lock, flags);
357 358 359 360 361

	return 0;
}

/**
362
 * pm_clk_resume - Enable clocks in a device's PM clock list.
363 364
 * @dev: Device to enable the clocks for.
 */
365
int pm_clk_resume(struct device *dev)
366
{
367
	struct pm_subsys_data *psd = dev_to_psd(dev);
368
	struct pm_clock_entry *ce;
369
	unsigned long flags;
370 371 372 373

	dev_dbg(dev, "%s()\n", __func__);

	/* If there is no driver, the clocks should remain disabled. */
374
	if (!psd || !dev->driver)
375 376
		return 0;

377
	spin_lock_irqsave(&psd->lock, flags);
378

379
	list_for_each_entry(ce, &psd->clock_list, node)
380 381
		clk_enable(ce->clk);

382
	spin_unlock_irqrestore(&psd->lock, flags);
383 384 385 386 387 388

	return 0;
}

#endif /* CONFIG_PM */

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
/**
 * enable_clock - Enable a device clock.
 * @dev: Device whose clock is to be enabled.
 * @con_id: Connection ID of the clock.
 */
static void enable_clock(struct device *dev, const char *con_id)
{
	struct clk *clk;

	clk = clk_get(dev, con_id);
	if (!IS_ERR(clk)) {
		clk_enable(clk);
		clk_put(clk);
		dev_info(dev, "Runtime PM disabled, clock forced on.\n");
	}
}

/**
 * disable_clock - Disable a device clock.
 * @dev: Device whose clock is to be disabled.
 * @con_id: Connection ID of the clock.
 */
static void disable_clock(struct device *dev, const char *con_id)
{
	struct clk *clk;

	clk = clk_get(dev, con_id);
	if (!IS_ERR(clk)) {
		clk_disable(clk);
		clk_put(clk);
		dev_info(dev, "Runtime PM disabled, clock forced off.\n");
	}
}

/**
424
 * pm_clk_notify - Notify routine for device addition and removal.
425 426 427 428 429 430 431 432 433
 * @nb: Notifier block object this function is a member of.
 * @action: Operation being carried out by the caller.
 * @data: Device the routine is being run for.
 *
 * For this function to work, @nb must be a member of an object of type
 * struct pm_clk_notifier_block containing all of the requisite data.
 * Specifically, the con_ids member of that object is used to enable or disable
 * the device's clocks, depending on @action.
 */
434
static int pm_clk_notify(struct notifier_block *nb,
435 436 437 438
				 unsigned long action, void *data)
{
	struct pm_clk_notifier_block *clknb;
	struct device *dev = data;
439
	char **con_id;
440 441 442 443 444 445

	dev_dbg(dev, "%s() %ld\n", __func__, action);

	clknb = container_of(nb, struct pm_clk_notifier_block, nb);

	switch (action) {
446
	case BUS_NOTIFY_BIND_DRIVER:
447
		if (clknb->con_ids[0]) {
448 449
			for (con_id = clknb->con_ids; *con_id; con_id++)
				enable_clock(dev, *con_id);
450 451 452 453
		} else {
			enable_clock(dev, NULL);
		}
		break;
454
	case BUS_NOTIFY_UNBOUND_DRIVER:
455
		if (clknb->con_ids[0]) {
456 457
			for (con_id = clknb->con_ids; *con_id; con_id++)
				disable_clock(dev, *con_id);
458 459 460 461 462 463 464 465 466 467 468 469
		} else {
			disable_clock(dev, NULL);
		}
		break;
	}

	return 0;
}

#endif /* !CONFIG_PM_RUNTIME */

/**
470
 * pm_clk_add_notifier - Add bus type notifier for power management clocks.
471 472 473 474
 * @bus: Bus type to add the notifier to.
 * @clknb: Notifier to be added to the given bus type.
 *
 * The nb member of @clknb is not expected to be initialized and its
475
 * notifier_call member will be replaced with pm_clk_notify().  However,
476 477 478
 * the remaining members of @clknb should be populated prior to calling this
 * routine.
 */
479
void pm_clk_add_notifier(struct bus_type *bus,
480 481 482 483 484
				 struct pm_clk_notifier_block *clknb)
{
	if (!bus || !clknb)
		return;

485
	clknb->nb.notifier_call = pm_clk_notify;
486 487
	bus_register_notifier(bus, &clknb->nb);
}